当发送ajax post请求时,数据将提供服务,而从响应中返回时,数据将进入上述错误函数,有人能在这方面帮助我吗
<!-- language: lang-js -->
$.ajax({
type: "POST",
url: serviceUrl,
data: stringData,
contentType: "application/json",
dataType: "text", // the data type we want back, so text. The data will come wrapped in xml
success: function(response) {
alert("successs :::::::::::::::" + $("#searchresultsA").html(data)); // show the string that was returned, this will be the data inside the xml wrapper
return response;
},
error: function(response) {
alert("error ::::::::::::" + response.responseText); // show the data inside the xml wrapper
return response.responseText;
},
failure: function(response) {
alert("failure ::::::::::::" + response);
return response;
}
});
}
发布于 2018-02-20 21:37:40
您应该在AngularJS中使用$http
而不是$.ajax
。您正在寻找关于AJAX响应为什么会出现错误的解决方案,而from you previous question,很明显,您需要知道如何正确地对数据进行解析。
以前使用的$http
(比$.ajax
更好)期望您的数据以 JSON 的形式到达,但是您的后端将其作为纯文本发送,从而给您一个JSON解析器错误。您需要指定如何发送此信息,以及是否将其编码为JSON。
对于PHP
有一个函数json_encode()
可以用来将对象/数组返回为JSON。在您的示例中尝试以下代码:
echo json_encode( $json );
验证您的响应
考虑使用console.log(response);
(在异步回调中)检查从后端接收到的内容。例如:
$http.post(url, stringData).
then((response)=>{
console.log(response);
},(error)=>{
console.log(error);
});
https://stackoverflow.com/questions/48885838
复制相似问题