2016-12-16 3 views
1

私はjQuery.ajax()を使用して、自分のPHPファイルにデータを送信し、MailChimpリストにユーザを追加します。jQuery AJAX SafariでHTTPエラーコードが取得されない

$.ajax({ 
    url: url, 
    method: 'POST', 
    data: params, // the data is working so here is just the variable I'm using 
    error: function(response) { 
    console.error($.parseJSON(response)); 
    // show some response text through DOM manipulation 
    }, success: function (response) { 
    console.log($.parseJSON(response)); 
    // show some response text through DOM manipulation 
    } 
}); 

PHPは、この(少なくとも、単にHTTPステータスコード応答のための)のようになります:私は良い応答(HTTPステータスを取得する場合、Chromeで

// All my PHP code, which is working is above this 
$result = curl_exec($ch); 
echo $result; // This is for the response text in the ajax call 
http_response_code(intval(json_decode($result)->status)); // This should be sending a success code or triggering errors 

Javascriptがこのようになります200の場合)、success関数が実行されます。私のテストで、400秒のHTTPステータスをエラーが発生した場合、error関数が実行されます。すべてが良いです。

しかし、Safariでは、それが200コードであれ400コードであれ、success関数が実行されます。 Safariブラウザを使用しているとき、Javascriptにエラーコードが検出されていないようです。これをどうすれば解決できますか? ChromeとSafariの違いは何ですか?

問題が発生した場合、私はCodeKitとMAMP PROを使ってプロジェクトを実行しています。ありがとう。

+1

PHPがどのように機能するかは奇妙です。私は応答コードが応答が送信される前に設定する必要があると思います。さもなければ、 "ヘッダーは既に送信されました"というエラーが出ます。 – apokryfos

+0

@apokryfosあなた天才!それを答えて、私はそれを受け入れる – ArtlyticalMedia

答えて

1

あなたはhttp_response_codeは、本質的に応答する前に送信するheader("HTTP/1.0 $code $codeMessage")headerニーズを実行しているので、これはすなわち、

$result = curl_exec($ch); 
http_response_code(intval(json_decode($result)->status)); // This should be sending a success code or triggering errors 
echo $result; // This is for the response text in the ajax call 

、実際の応答の前に応答コードを送信する必要があります。

関連する問題