Javascript AJAX请求json失败
我对ajax请求有一个问题,当我使用属性数据类型“json”执行请求时,我的响应会出现一个错误,parsererror,我在PHP中的函数会返回json_encode()之类的数据,请帮我一下好吗?当我在没有属性数据类型:“json”的情况下执行请求时,我的数据是所有文档HTML 我的请求:Javascript AJAX请求json失败,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我对ajax请求有一个问题,当我使用属性数据类型“json”执行请求时,我的响应会出现一个错误,parsererror,我在PHP中的函数会返回json_encode()之类的数据,请帮我一下好吗?当我在没有属性数据类型:“json”的情况下执行请求时,我的数据是所有文档HTML 我的请求: var dataAr = {Latitude: Latitude, Longitude: Longitude};/ console.log(d
var dataAr = {Latitude: Latitude, Longitude: Longitude};/
console.log(dataAr);
$.ajax({
data: dataAr,
dataType: 'json',
type: 'POST',
url: 'http://localhost/GPS/Server.php/GPS/Coords',
success: function (data, response) {
console.log('Data: '+data);
console.log('Response: '+response);
},
error: function (textStatus, errorThrown) {
console.log('Status: '+textStatus);
console.log('Error: '+errorThrown);
}
});
我在PHP中的函数:
class GPS
{
function Coords()
{
$Res=$_POST['data'];
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
return json_encode($Res);
}
}
尝试使用
内容类型
:
function Coords()
{
$Res=$_POST['data'];
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
header('Content-Type: application/json'); // <-- HERE
return json_encode($Res);
}
函数Coords()
{
$Res=$_POST['data'];
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
header('Content-Type:application/json');//在您发送的内容中,$\u POST变量具有相同的名称,而不是“data”。不清楚您试图返回什么,因此,出于示例目的,以下仅返回并数组输入值:
class GPS
{
function Coords()
{
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
$result = array( $Latitude, $longitude );
header('Content-Type: application/json');
echo json_encode($result);
}
}
您是否使用echo将数据发送回Ajax函数?@bobdye与我的函数PHP中的echo发生相同的错误检查浏览器控制台网络选项卡中的实际请求。我怀疑您的路由不正确either@charlietfl你是在说我的URL吗?我不这么认为,但谢谢你的评论,我只是在本地测试了我的请求e类似于没有类和函数的“server.php”,并且…不起作用