【发布时间】:2015-01-08 08:10:00
【问题描述】:
我使用$.ajax() jquery 来做一些ajax 调用。最近我有一个 ajax 调用,它返回 json 格式的字符串以获得成功的调用。在我的服务器 php 代码中,如果有问题,我只返回一个字符串数据 (die("nothing found in database");),非 json 格式,这是一条解释问题的消息。 ajax 调用工作正常,但我很难得到那个字符串响应。
这里是javascript代码:
$.ajax({
url: "check.php",
dataType: "json",
data: {
row_id : row_id
},
async: false
}).done(function(data) {
check = data;
}).fail(function( jqXHR, textStatus, msg ){
$('#dialog').html("<p>"+textStatus+"</p>");
$('#dialog').dialog({
modal: true,
buttons: {
"OK": function() {
$( this ).dialog("close");
}
}
});
});
.fail() 函数中的 3 个变量都没有给我从服务器发送的字符串响应。 textStatus 只是给了我“parseerror”。 msg 变量给我:“意外的令牌 T”。我想要的只是我使用die() 函数从.php 文件发送的纯字符串。我测试以确保 php 正确发送字符串。有谁知道我应该如何获得我的字符串数据响应?
也许这不是处理这个问题的正确方法。我在想是因为 ajax 调用确实没有错误,只是我选择返回一个正常的字符串而不是 JSON,这是一个有问题的信号。
【问题讨论】:
-
在
fail中添加一个alert(textStatus),看看你得到了什么。如果<p>中的值无效,则可能导致html()行失败。
标签: jquery ajax json error-handling