2011-08-09 45 views
3

我在Chrome中遇到了錯誤Uncaught SyntaxError: Unexpected token ILLEGALJavascript:意外的令牌非法

的代碼是

$("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

放'的console.log(res.responseText);'JSON.parse前並顯示輸出。 – iafonov

+0

從你的ajax調用返回的結果是不好的。解析器正在踢出錯誤,因爲響應是無效的json。正如馬克建議嘗試通過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已經解析響應時試圖解析響應時出現錯誤。區別在於「成功」的回調;我刪除了這一行: 'var response = JSON.parse(res.responseText);' – circusbred

0

要在意見的一個擴展上面,我正因爲正在返回的JSON結果的問題的這一錯誤。具體來說,JSON響應數據中的一個字符串值在其中包含一個未加引號的雙引號。在我的情況下,這是我自己調用的Ajax函數,所以我修正它的方法是在返回JSON數據之前轉義服務器上的雙引號。後來我發現,我有換行符同樣的問題,所以我用了str_replace函數調用,我在另一篇文章中發現: 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; 
}