I want to send JSON array via ajax call to a web service PHP. I've tried a lot of solution (most of the suggested similar answers for such question) with no clue.
My JSON array structure is:
var res= [{"id":-9007199254740990,
"NW":{"x":3.97,"y":5.83},
"SE":{"x":2.72,"y":3.53},
"NE":{"x":1.97,"y":8.83},
"SW":{"x":3.87,"y":4.83}}]
and the JavaScript function that handles the request is as follows:
send_json(res); //call function
function send_json(res)
{
var myJsonString = JSON.stringify(res);
console.info(myJsonString);
$.ajax({
url:"test.php", //the page containing php script
type: "GET", //request type
contentType: "application/json; charset=UTF-8",
data: {data : myJsonString} ,
success:function(result){
//JSON version
console.info(result);
}
});
}
The php file handles the request as follows:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
$data = json_decode(file_get_contents("php://input"));
print_r($data);
}
?>
I've tried also to send the JSON as key value JS:
data: {data: myJsonString}
and in turn received it in php via:
if(isset($_GET['data']))
{
echo json_decode($_GET['data']);
}
With no clue as well, the output is empty in both trials.