Php 使用echo时出现jquery ajax json请求错误
我有以下代码:Php 使用echo时出现jquery ajax json请求错误,php,jquery,json,request,Php,Jquery,Json,Request,我有以下代码: $.ajax({ url: 'urlsubmit.php', data: json_data, dataType: 'json', type: 'POST', success: function(data) { $('#saveResult_'+tnTopic_id).html("OK"); if(da
$.ajax({
url: 'urlsubmit.php',
data: json_data,
dataType: 'json',
type: 'POST',
success: function(data) {
$('#saveResult_'+tnTopic_id).html("OK");
if(data.indexOf("ok") !== -1) {
$("#saveResult_"+tnTopic_id).html("Ok, am salvat!");
}
else {
$("#saveResult_"+tnTopic_id ).html(data);
//alert("A aparut o eroare:\n\r"+data);
}
},
error: function (jqXHR, textStatus, errorThrown) {
$('#saveResult_'+tnTopic_id).html(textStatus + ' | ' + errorThrown + ' | ' + jqXHR.responseText);
//alert(xhr.status);
//alert(thrownError);
}
});
}
而在urlsmit.php中,我只有echo“ok”
我得到了这个错误parsererror | SyntaxError:Unexpected token o删除这个-(如果您没有用json响应)
jQuery试图将
ok
解析为json,这就是为什么会出现错误的原因dataType:'json'。。。它是there@Zvunks他说要删除它,dataType
就是告诉jQuery作为响应应该尊重哪种类型的数据。因为您指定了json
,所以它试图将其解析为json
-“ok”
不是有效的json,并引发错误。如果删除dataType
属性,它将自动猜测。如果确实要发送json数据,请使用“echo json_encode($your_变量);”从您的PHP
dataType: 'json',