jQuery AJAX - 意外令牌 + 解析器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7457044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AJAX - Unexpected token + parsererror
提问by Skylineman
I wrote a script using jQuery and AJAX today, and I get some errors...
我今天使用 jQuery 和 AJAX 编写了一个脚本,但出现了一些错误...
The script:
剧本:
function changeAdmin(id) {
$(document).ready(function() {
$('#ta-modarea-'+id).fadeOut('fast');
$('#ta-m-loading-'+id).fadeIn('fast');
$.ajax({
type: 'POST',
url: 'ajax_utf.php?a=changeteamadmin',
dataType: 'json',
data: {
admin : $('#admin-id-'+id).val()
},
success: function(data) {
$('#ta-m-loading-'+id).fadeOut('fast');
$('#ta-modarea-'+id).text(data.msg).fadeIn('fast');
},
error: function(jqXHR, textStatus, errorThrown) {
$('#ta-m-loading-'+id).fadeOut('fast');
$('#ta-modarea-'+id).text('HTTP Error: '+errorThrown+' | Error Message: '+textStatus).fadeIn('fast');
}
});
return false;
});
}
After the run, I get this error message: HTTP Error: SyntaxError: Unexpected token < | Error Message: parsererror
运行后,我收到此错误消息: HTTP Error: SyntaxError: Unexpected token < | Error Message: parsererror
Could you help me, what should I do?
你能帮我吗,我该怎么办?
回答by Wrong
You need to send an application/json
header via PHP , like this:
您需要application/json
通过 PHP发送标头,如下所示:
header('Content-type: application/json');
That's because jQuery sends an Accept header (application/json, text/javascript
), and this is the cause of parseerror
triggered by jqXHR
.
那是因为 jQuery 发送了一个 Accept 标头 ( application/json, text/javascript
),这就是parseerror
触发jqXHR
.
回答by Alon Eitan
Try
尝试
alert( jqXHR.responseText);
in your error function
在你的错误函数中
回答by Andrew Killen
If you have tried setting the header content type and are still getting the error. It is my expectation that the server is replying with a fault from your server side code. Usually when a debug message is given it is in pure HTML not JSON thus the unexpected token.
如果您已尝试设置标头内容类型,但仍然出现错误。我期望服务器会从您的服务器端代码中回复错误。通常,当给出调试消息时,它是纯 HTML 而非 JSON,因此是意外标记。
The quickest way to debug this is to set the DataType of the HTML instead of JSON so that you can see whatever output there is from the server, not just JSON formatted data.
调试此问题的最快方法是设置 HTML 的 DataType 而不是 JSON,这样您就可以看到来自服务器的任何输出,而不仅仅是 JSON 格式的数据。
Once you have seen the error that is being produced by your server side code and fixed it, you can then return to being a DataType of JSON.
一旦您看到服务器端代码产生的错误并修复它,您就可以恢复为 JSON 的数据类型。
回答by Sudhir
contentType: "application/json; charset=utf-8",
contentType: "application/json; charset=utf-8",
回答by rrr_2010
Try code below, but if you receive an error like "Unexpected token <", you need to check your php file - "ajax_utf.php" and check what is returned in browser (Chrome) View->Developer->Developer Tools, Network tab -> XHR.
尝试下面的代码,但如果您收到“意外令牌 <”之类的错误,您需要检查您的 php 文件 - “ajax_utf.php”并检查浏览器 (Chrome) View->Developer->Developer Tools, Network 中返回的内容选项卡 -> XHR。
$.ajax({
type: 'post',
url: postLink,
dataType: 'json',
data: postData,
beforeSend: function (x) {
if (x && x.overrideMimeType) {
x.overrideMimeType('application/json;charset=UTF-8' );
}
},
success: function (result) {
//console.log(result);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(arguments);
}
});
回答by Digitum
It could be an issue with missmatching PHP associative/numeric arrays and Javascript objects.
这可能是 PHP 关联/数字数组和 Javascript 对象不匹配的问题。
Try this:
尝试这个:
$data = new Array();
$data['test'][] = "Row 1";
$data['test'][] = "Row 2";
echo json_encode($json, JSON_FORCE_OBJECT);
This should force json encoder to always encode to objects instead of numeric arrays and may solve the problem.
这应该强制 json 编码器始终编码为对象而不是数字数组,并且可能会解决问题。