2015-09-17 43 views
6

これは本当に私の心を揺さぶっています。私はajaxからエラーコールバックを取得します。しかし、エラーメッセージからres.responseText(正しい、btwに戻ってくる)を取り出して使用すると、正しいことが実行されます。ちょうど私が成功コールバックを受け取ったかのように。readystate 4、ステータス200、statustext okでエラーを返すjquery ajaxコール

データは次のように設定されている:

var dataToSend = {fieldname : textdata}; 

とAjaxの呼び出しは、このようなものです:

var ajaxOptions = { 
    url: '/newpage', 
    data: JSON.stringify(dataToSend), 
    contentType: 'application/json; charset=utf-8', 
    dataType: 'json', 
    cache: false, 
    processData: false, 
    type: 'POST', 
    success: function(res) { 
     console.log("success!"); 
     $('#' + divname).html(res); 
    }, 
    error: function(res) { 
     console.log("There was an error: " + JSON.stringify(res)); 
     $('#' + divname).html(res.responseText); 
    } 
}; 

$.ajax(ajaxOptions); 

エラーメッセージは次のとおりです。エラーが発生しました:{"readyState":4,"responseText" [this part is perfectly fine], "status":200, "statusText":"OK"}

+0

'dataType: 'json'、'これはソースからの応答です。あなたが打つソースがjsonを結果として生み出していることを確認できますか?それを取り除いた後で試してみてください。 – Jai

+1

はあなたがtext/html ....を返すように見えるので、エラーはおそらくjson解析エラーです。 'dataType: 'json''を削除してください。実際に返されたものを表示する – charlietfl

+0

@Jai - それは良い推測でした!私は自分のデータベースのJSONデータに型を変更しましたが、私はまだ同じエラーが発生します。 – lynvie

答えて

8

あなたのresponseTextが正しいJSONでない場合、解析エラーがスローされます。 応答が有効なJSONまたはであることを確認して、dataType: "json"を削除してください。 jQuery docsから

dataType (default: Intelligent Guess (xml, json, script, or html))

Type: String

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

...

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

1

これは、あなたが応答を取得するdataTypeセットを持っている場合であることを起こるが、応答では、データ型に設定されたものではありません。

あなたのケースでは、dataType: 'json',が設定されており、コメントセクションで述べたように、バックエンドにstringを設定しているので、データ型をtextに変更する必要があります。

にあなたのデータ型を変更

:問題のサーバーサイドを解決する1つの方法は、エンコードされた空の配列、JSONをエコーすることです

dataType: 'text', 
+0

私はmongodbを使用しています。私は{type:Schema.Types.Mixed}を持っています。私がconsole.logにvar dataToSendが出力するもの: "title": "This title。" JSON.stringify(dataToSend)で送信します。 JSONデータでは不十分ですか? – lynvie

+1

@lynvie、あなたが持っている問題は、送信されたデータではなく、応答のデータである。あなたは 'console.log(responseText)'を実行し、それがJSONかどうかをチェックしなければなりません – Diego

0

echo json_encode([]); 
return; 

そして、成功関数が代わりにエラーのトリガーます。

または、スクリプト側dataType: 'json',dataType: 'text',に変更して、jqueryにテキスト応答を受け取るように指示してください。

関連する問題