如何解析来自serialize()的错误和状态消息?
我有这段代码
$(document).ready(function(){
// ...
$('form').submit(function() {
// ...
$.ajax({
type: "GET",
url: "/cgi-bin/ajax.pl",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: $(this).serialize(),
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('div#create_result').text(
"responseText: " + XMLHttpRequest.responseText +
", textStatus: " + textStatus +
", errorThrown: " + errorThrown);
$('div#create_result').addClass("error");
}, // error
success: function(result){
if (result.error) { // script returned error
$('div#create_result').text("result.error: " + result.error);
$('div#create_result').addClass("error");
} // if
else { // perl script says everything is okay
$('div#create_result').text(
"result.success: " + result.success +
", result.userid: " + result.userid);
$('div#create_result').addClass("success");
} //else
} // success
}); // ajax
$('div#create_result').fadeIn();
return false;
});
});
,如果成功的话,它总是给出错误消息。
示例:
responseText: Content-Type: application/json; charset=utf-8 {"success" : "Activity created", "Activity number" : "38"}, textStatus: parsererror, errorThrown: SyntaxError: JSON.parse
有什么想法是错的吗?
更新:
以下是我在服务器端 Perl 脚本中制作 JSON 字符串的方法。
...
$json = qq{{"error" : "Owner $owner doesn't exist"}};
...
$json = qq{{"success" : "Activity created", "Activity number" : "$id"}};
...
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;
I have this code
$(document).ready(function(){
// ...
$('form').submit(function() {
// ...
$.ajax({
type: "GET",
url: "/cgi-bin/ajax.pl",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: $(this).serialize(),
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('div#create_result').text(
"responseText: " + XMLHttpRequest.responseText +
", textStatus: " + textStatus +
", errorThrown: " + errorThrown);
$('div#create_result').addClass("error");
}, // error
success: function(result){
if (result.error) { // script returned error
$('div#create_result').text("result.error: " + result.error);
$('div#create_result').addClass("error");
} // if
else { // perl script says everything is okay
$('div#create_result').text(
"result.success: " + result.success +
", result.userid: " + result.userid);
$('div#create_result').addClass("success");
} //else
} // success
}); // ajax
$('div#create_result').fadeIn();
return false;
});
});
which always gives error messages, if it was successful.
Example:
responseText: Content-Type: application/json; charset=utf-8 {"success" : "Activity created", "Activity number" : "38"}, textStatus: parsererror, errorThrown: SyntaxError: JSON.parse
Any ideas what is wrong?
Update:
Here is how I make the JSON strings in the server-side Perl script.
...
$json = qq{{"error" : "Owner $owner doesn't exist"}};
...
$json = qq{{"success" : "Activity created", "Activity number" : "$id"}};
...
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这:
解析器不接受您的响应 JSON,它可能格式错误。您发布的
responseText
中包含标题,这些标题不应出现。:This:
Your response JSON is not acceptable to the parser, it might be malformed. Your posted
responseText
has headers in it, those should NOT be present.:考虑使用 Perl 的 JSON 模块,它为您处理序列化和解析。有一个 XS 后端(为了速度),以及一个 PP(纯 Perl)后端。
Consider using Perl's JSON module, which handles serialization and parsing for you. There is an XS backend (for speed), as well as a PP (Pure Perl) backend.