AJAX请求json失败


AJAX request json fail

我的ajax请求有问题,当我用属性dataType:'json'进行请求时,我的响应出现了一个错误,parserror,我在PHP中的函数返回了类似json_encode()的数据,你能帮我吗?当我在没有属性dataType:'json'的情况下执行请求时,我的数据是ALL the DOCUMENT HTML。

我的请求:

                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);            
    }
}

尝试使用content-type:

function Coords()
{
    $Res=$_POST['data'];
    $Latitude=$_POST['Latitude'];
    $Longitude=$_POST['Longitude'];
    header('Content-Type: application/json'); // <-- HERE
    return json_encode($Res);            
}

$_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);            
    }
}