2011-08-09 17 views
3

ChromeにエラーUncaught SyntaxError: Unexpected token ILLEGALがあります。Javascript:予期しないトークンILLEGAL

コードがエラーで

$("form#new_redemption").live('submit', function() { 
    event.preventDefault(); 
    var that = $(this); 

    var action = that.attr('action'); 
    var data = that.serialize(); 

    $.ajax({ 
    type: "POST", 
    url: action, 
    data: data, 
    dataType: 'json', 
    beforeSend: function(request) { 
     request.setRequestHeader("Accept", "application/json"); 
    }, 
    success: function(res) { 
     var response = JSON.parse(res.responseText); // <~~~ Unexpected token ILLEGAL 
     if (response.message) { 
     that.slideUp(); 
     $("#results").html(response.message).attr('class', 'notice').slideDown(); 
     } 
     else if (response.url) { 
     window.location = response.url 
     } 
    }, 
    error: function(res) { 
     var response = JSON.parse(res.responseText); 
     $('#results').html(response.error).attr('class', 'error').slideDown(); 
    } 
    }); 
    return false; 
}); 

で、このコードは素晴らしい作品。しかし、成功するたびにエラーが出ます。ここに問題はありますか?そして、VIMにコード内の違法なJavaScript文字を強調する方法がありますか?

ありがとうございました!

+2

を;' JSON.parse前に出力を表示します。 – iafonov

+0

あなたのajax呼び出しから返ってくる結果は良いことではありません。応答が無効なjsonであるため、パーサーがエラーを蹴っています。 Markが示唆するように、結果をjsonバリデーターで実行しようとします。 – kasdega

+0

'res.responseText'に' undefined'があります。成功した回答のための別のプロパティはありますか? –

答えて

2

設定dataTypejsonは、successコールバック内の応答JSONを自動的に解析します。

これを試してみてください:

$("form#new_redemption").live('submit', function() { 
    event.preventDefault(); 
    var that = $(this); 

    var action = that.attr('action'); 
    var data = that.serialize(); 

    $.ajax({ 
    type: "POST", 
    url: action, 
    data: data, 
    dataType: 'json', 
    beforeSend: function(request) { 
     request.setRequestHeader("Accept", "application/json"); 
    }, 
    success: function(res) { 
     if (response.message) { 
     that.slideUp(); 
     $("#results").html(response.message).attr('class', 'notice').slideDown(); 
     } 
     else if (response.url) { 
     window.location = response.url 
     } 
    }, 
    error: function(res) { 
     var response = JSON.parse(res.responseText); 
     $('#results').html(response.error).attr('class', 'error').slideDown(); 
    } 
    }); 
    return false; 
}); 
+0

ここでは何が起こっていますか?私はこれが彼の問題を解決するのを見るのですが、なぜですか? –

+0

@TobyAllen OPコードでは見えませんが、目に見えない文字が問題を引き起こしている可能性があります。 [この質問](http://stackoverflow.com/q/12719859/825789)を参照してください。 – bfavaretto

+0

リンクされた質問には関係ありません。エラーは、jQueryがすでに応答を解析しているときに応答を解析しようとしたときに発生します。違いは 'success'コールバックにあります。私はこの行を削除しました: 'var response = JSON.parse(res.responseText);' – circusbred

0

は私が原因で返されたJSONの結果でこの問題のエラーを取得して、上記のコメントの一つに展開します。具体的には、JSON応答データ内の文字列値の1つに、二重引用符がエスケープされていませんでした。私の場合、それは自分のAjax関数であり、JSONデータを返す前にサーバーの二重引用符をエスケープすることでした。それから私は、私は改行文字と同じ問題を抱えていたことを発見したので、私は、私は別のポストで見つかったstr_replace呼び出しを使用: `はconsole.log(res.responseText)入れ PHP's json_encode does not escape all JSON control characters

function escapeJsonString($value) { 
    # list from www.json.org: (\b backspace, \f formfeed)  
    $escapers =  array("\\",  "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); 
    $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); 
    $result = str_replace($escapers, $replacements, $value); 
    return $result; 
} 
関連する問題