[英]Parserror in AJAX request, form submit
I'm using an AJAX
request to submit
a form
login. 我正在使用
AJAX
请求submit
form
登录名。
$.post(url, {data: JSON.stringify( {obj: value} )}, 'json')
.fail(function(){
console.log(typeof arguments[0].responseText) //logs 'string'
//console.log(JSON.parse(arguments[0].responseText)) // decommented logs "unexpected token"
console.log(arguments)
});
I'm getting 我越来越
1: "parsererror"
2: SyntaxError
message: "Unexpected token "
..
..
I've also set header("Content-type: application/json")
but it did not solved the problem, I'm also using json_encode
as server-side response. 我还设置了
header("Content-type: application/json")
但它没有解决问题,我还使用json_encode
作为服务器端响应。
I got status: 200
and, it seems, the right json
responseText
. 我的
status: 200
,似乎是正确的json
responseText
。 I don't know what else to do. 我不知道该怎么办
(do not flag this question as a duplicate, I did search over SO, none question solved my issue) (不要将此问题标记为重复,我确实在SO上进行了搜索,没有一个问题解决了我的问题)
EDIT Added responseText
编辑添加了
responseText
chrome 铬
responseText: "↵{"success":true,"error":false}"
firefox 火狐
"\r\n{"success":true,"error":false}"
EDIT2 EDIT2
json_encode(array( .. ))
introduce \\r\\n
but I don't know why. 介绍
\\r\\n
但我不知道为什么。
↵
is unexpected char in response, please use ob_start
function before json_encode function on server side. ↵
是为了应对意外的字符,请使用ob_start
在服务器端json_encode功能之前功能。 ↵
this is enter
key code, please remove from response ↵
这是enter
关键代码,请从响应删除
ltrim(json_encode($response_arr), "\r\n"); // this would be useful in php code
// //
responseText: "↵{"success":true,"error":false}"
to 至
responseText: "{"success":true,"error":false}"
The issue in ajax response, see blow image for better understanding Ajax响应中的问题,请参见打击图像以更好地理解
new line
case (red mark) will have in your server code.. if you can remove new line
the issue will be resolved. 服务器代码中将包含
new line
(红色标记)..如果可以删除new line
该问题将得到解决。
Other solution 其他解决方案
remove header( Content-type : application/json
) and use jQuery.parseJSON(jQuery.trim(response))
in jQuery code. 删除header(
Content-type : application/json
)并在jQuery代码中使用jQuery.parseJSON(jQuery.trim(response))
。
ajax code 阿贾克斯代码
$.post(url, {data: JSON.stringify( {obj: value} )}, function(response){
var data = jQuery.parseJSON(jQuery.trim(response));
console.log(data);
})
.fail(function(){
console.log(typeof arguments[0].responseText) //logs 'string'
//console.log(JSON.parse(arguments[0].responseText)) // decommented logs "unexpected token"
console.log(arguments)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.