jQuery AJAX - 在本地主机上使用 XAMPP 时未收到 JSON 数据
jQuery AJAX - Not receiving JSON data when on localhost using XAMPP
我正在使用此代码:
$.ajax({
type: 'post',
url: "http://www.localhost/do_getmemes.php",
dataType: 'json',
data: {userid: userid, lastid: lastID},
success: function(data) {
console.log('bla');
console.log(data);
}
});
在 do_getmemes.php
中成功接收到 post 参数并且正在生成 json 但我没有在 success
上得到它?控制台没有显示任何内容。它在网站上运行良好,但在本地主机上使用 XAMPP
时却不行
这一切都在 php 文件中,这是在最后:
file_put_contents('test.json', json_encode($array)); // file generated and not empty
echo json_encode($array);
这里有什么问题?
编辑:
AJAX 通常有效,我通过获取简单字符串进行测试:
$.ajax({
url: "http://www.localhost/contact/text.php",
success: function(data) {
console.log(data) // got it
}
});
问题是不相关的警告,这些警告也通过 API 返回并导致 parsererror SyntaxError: Unexpected token < in JSON at position 0
错误。
除了修复它们之外,这是确保 API 遗嘱有效的方法:
禁用 PHP 文件中的警告:
error_reporting(0);
ini_set('display_errors', 0);
我正在使用此代码:
$.ajax({
type: 'post',
url: "http://www.localhost/do_getmemes.php",
dataType: 'json',
data: {userid: userid, lastid: lastID},
success: function(data) {
console.log('bla');
console.log(data);
}
});
在 do_getmemes.php
中成功接收到 post 参数并且正在生成 json 但我没有在 success
上得到它?控制台没有显示任何内容。它在网站上运行良好,但在本地主机上使用 XAMPP
这一切都在 php 文件中,这是在最后:
file_put_contents('test.json', json_encode($array)); // file generated and not empty
echo json_encode($array);
这里有什么问题?
编辑:
AJAX 通常有效,我通过获取简单字符串进行测试:
$.ajax({
url: "http://www.localhost/contact/text.php",
success: function(data) {
console.log(data) // got it
}
});
问题是不相关的警告,这些警告也通过 API 返回并导致 parsererror SyntaxError: Unexpected token < in JSON at position 0
错误。
除了修复它们之外,这是确保 API 遗嘱有效的方法:
禁用 PHP 文件中的警告:
error_reporting(0);
ini_set('display_errors', 0);