2

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.

16
  • Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server? Commented Dec 13, 2016 at 13:09
  • @JayBlanchard all dependencies are included, and i have another function that instantiate another ajax call but with key-valued data, and it works fine Commented Dec 13, 2016 at 13:13
  • That still doesn't answer the questions. Have you watched the request / response? Commented Dec 13, 2016 at 13:13
  • from where can i watch them ? i can only see the error generated in the console tab Commented Dec 13, 2016 at 13:18
  • Look at the network tab in the developer tools. Commented Dec 13, 2016 at 13:19

2 Answers 2

1
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}]

This is an array of json object, not a string You can pass a json object as it is

data:res,
type: "post",

Now in server do

print_r($_POST);
Sign up to request clarification or add additional context in comments.

4 Comments

i know that it not a string, it's just a name for the array :S I did so, and the outout was an empty array()
The OP converts to a string here var myJsonString = JSON.stringify(res);
@JayBlanchard this is the way i know to encode an array to JSON, do you have any other ?
No, this is the correct way to convert to a string @JafarAlali92. I was just pointing out to Ima that you had done the conversion.
0

Don't use GET if you are trying to send data to the server, use post instead

type: "post",

2 Comments

With jQuery AJAX you can send data via a GET request. It looks strange, but it works.
You can use a hammer to open a door too, but it's not the purpose of the GET method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.