Javascript:意外的令牌非法
我在 Chrome 中遇到错误 Uncaught SyntaxError: Unexpected token 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 字符?
谢谢你!
I have the error Uncaught SyntaxError: Unexpected token ILLEGAL
in Chrome.
The code is
$("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;
});
On errors, this code works great. But every time its a successful response I get an error. Is there a problem here? And is there a way in VIM to highlight illegal javascript characters in the code?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
dataType
设置为json
将在success
回调中自动解析响应 JSON。试试这个:
Setting
dataType
tojson
will automagically parse the response JSON for you within thesuccess
callback.Try this:
为了扩展上面的评论之一,我收到此错误是因为返回的 JSON 结果存在问题。具体来说,JSON 响应数据中的字符串值之一包含未转义的双引号。就我而言,我调用的是我自己的 Ajax 函数,因此我修复它的方法是在返回 JSON 数据之前转义服务器上的双引号。然后我发现换行符也有同样的问题,所以我使用了在另一篇文章中找到的 str_replace 调用:
PHP 的 json_encode 不会转义所有 JSON 控制字符
To expand on one of the comments above, I was getting this error because of a problem in the JSON results that were being returned. Specifically, one of the string values in the JSON response data had an unescapted double quote in it. In my case, it was my own Ajax function that I was calling, so what I did to fix it was to escape the double quotes on the server before returning the JSON data. Then I discovered that I had the same problem with newline characters, so I used a str_replace call that I found in another post:
PHP's json_encode does not escape all JSON control characters