2016-04-23 8 views
1

PHPからAJAX Postと対話して値を取得しようとしています。 私はJSONデータ型を使うべきだと読んだことがありますが、これは私にとって初めてのことです。JSONデータの1行目72列のJSONデータの後に "SyntaxError:JSON.parse:予期しない非空白文字"が表示されます。PHPはAJAX Post-Returnの値を返します。JSON

私のAJAX以下の通りです:

function apfaddpost() { 
     var fd = new FormData($('#msform')[0]); 
     fd.append("main_image", $('#main_image')[0].files[0]); 
     fd.append("action", 'apf_addpost'); 
     $('#form-container').hide(); 
     $('#processing').show(); 
     var postProject = $.ajax({ 
      type: 'POST', 
      url: apfajax.ajaxurl, 
      data: fd, 
      dataType: 'json', 
      processData: false, 
      contentType: false, 
     }); 

     postProject.done(function(data, textStatus, XMLHttpRequest) { 
       $('#processing').hide(); 
       $('#confirm').show(); 
       //elements where I should display success message and link 
       var success = '#success'; 
       var projectlink = '#projectlink'; 
       jQuery(success).html(''); 
       jQuery(success).append(data.success); 
       $("#projectlink").attr("href", data.projectlink); 
     }); 

     postProject.fail(function(MLHttpRequest, textStatus, errorThrown) { 
       alert(errorThrown); 
     }); 
    } 

マイPHP

if ($pid != 0) 
    { 
     $message = 'Your post has been successfully added!'; 
     $project_link = get_permalink($pid); 
     $result = array('success'=>$message,'projectlink'=>$projectlink); 
     echo json_encode($result); 
    } 
    else { 
     $message = 'Error occurred while adding the post'; 
     $result = array('fail'=>$message); 
     echo json_encode($result); 
    } 

それらの値を印刷しなければならない私のHTMLは次のとおりです。

私は
<div id="confirm" class="row" style="display:none"> 
     <div class="col-sm-12"> 
      <h2 class="text-center"><?php _e("Thank you!","KleeiaDev") ?></h2> 
      <div class="text-center"> 
       <p id="success"></p><!-- Here should go the success message --> 
       <p> 
        <a id="projectlink" href="">Link</a><!-- Here should go the link I'm getting as result --> 
       </p> 
      </div> 
     </div> 
    </div> 

違う?

+0

ajaxの応答は何ですか?応答を確認してください。 – WhoAmI

答えて

1

PHPがecho json_encode($result);の後に別のものを出力している場合は、そのエラーが発生します。

他に何も出力されていないことを確認してください。 json_encodeの後にアプリケーションロジックがない場合exitを使用します。

+1

助けてくれてありがとう!解決済み!乾杯。 – XiLab

関連する問題