So I sent this ajax request to php:
$.ajax({
url: "php/getInfo.php",
type: 'POST',
dataType: 'json',
data: {
lat: 51.4818048,
log: -0.5275648
},
success: function(result) {
console.log(result);
if (result.status.name == "ok") {
}
},
error: function(jqXHR, textStatus, errorThrown) {}
})
this is the php:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$executionStartTime = microtime(true) / 1000;
$url=`https://api.opencagedata.com/geocode/v1/json?q=` . $_REQUEST['lat'] . `+` . $_REQUEST['log'] . `MY API KEY`;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$decode = json_decode($result,true);
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "mission saved";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
$output['data'] = $decode['geonames'];
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($output);
?>
I replaced the API key that I have, the problem is in the response for the data key I get null.
I know it might be a foolish question and I apologise in advance.
My Answer:
"example.php",
method: "POST",
data: {name: "John", age: 30},
success: function(response) {
console.log(response);
}
});
And in my PHP file (example.php), I tried to get the values sent by Ajax like this:
$name = $_POST['name'];
$age = $_POST['age'];
echo "Name: " . $name . ", Age: " . $age;
But for some reason, the values are not being received by PHP. What could be the issue here?
Rate this post
4 of 5 based on 3166 votesComments