我有以下代码
$.ajax({
url: "../profile/companyAutocomplete.php",
type: "GET",
data: dataQuery,
dataType: "json",
success: function(responseData) {
companySelectHandler(responseData);
}
});
我在生产服务器中毫无问题地被调用。但是当我在我的本地系统中使用它时,并没有发出ajax请求。我在所有浏览器中都试过了,但还是一样。导致问题的原因是什么?我需要为此打印 ajax 错误消息。我怎样才能做到这一点?
最佳答案
感谢您的回答。这不是因为相对 URL 引用。
我使用以下函数找出导致 Ajax 请求失败的错误。
$(function() {
$.ajaxSetup({
error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
错误是一个解析错误,当浏览器试图在返回 JSON 之前打印其他内容时生成。它是通过在输出 JSON 之前使用 ob_start 和 ob_end_clean 修复的,JSON 通过从以下链接“dataType: "json" won't work”获取帮助来清除缓冲区
关于javascript - ajax 请求在 localhost wampserver 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30765960/