I have a problem with an ajax request, when I do the request with the property dataType: 'json', my response come with an error, parsererror, my function in PHP returns the data like json_encode(), pls, can you help me? when I do the request without property dataType: 'json' my data is ALL THE DOCUMENT HTML.
My request:
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);
}
});
My function in PHP:
class GPS
{
function Coords()
{
$Res=$_POST['data'];
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
return json_encode($Res);
}
}
Try using content-type
:
function Coords()
{
$Res=$_POST['data'];
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
header('Content-Type: application/json'); // <-- HERE
return json_encode($Res);
}
The $_POST variables have the same names at what you send in, not 'data'. It's not clear what you are trying to return, so for example purposes the following just returns and array with the input values:
class GPS
{
function Coords()
{
$Latitude=$_POST['Latitude'];
$Longitude=$_POST['Longitude'];
$result = array( $Latitude, $longitude );
header('Content-Type: application/json');
echo json_encode($result);
}
}