Hi !
I'm searching for days now and i can't find the mistake, i send my json :
var xhr = Ti.Network.createHTTPClient(); xhr.open("POST", "http://****.php"); xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); var postData = { object: { child1:'value', child2:'value' } } xhr.send(JSON.stringify(postData));And in my php, i tried :
$data = json_decode(stripslashes($_POST['postData']), true);
$data = json_decode(stripslashes($_POST['object']), true);
$data = json_decode($_POST['postData']);
$data = json_decode($_POST['object']);
But $data is always null, where did i fail ?
TY
5 Answers
Accepted Answer
Try xhr.send({myData:JSON.stringify(postData)}); and $data = json_decode($_POST['myData']);
Problem 1: don't stringify your POST data, just send it directly, like this:
var xhr = Ti.Network.createHTTPClient(); xhr.open("POST", "http://****.php"); xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); var postData = { object: { child1:'value', child2:'value' } } xhr.send(postData);Problem 2: the server won't see anything called 'postData'. That's just a variable name in your javascript. The server script's $_POST data will look like this:
object: { child1 = value; child2 = value; }So to use it, you would just do this:
$child1 = $_POST['child1']; $child2 = $_POST['child2'];
Jason is right, also var_dump($_POST); is your choice to check the passed data before trying to access it
TY Jason, Shannon and Anthony, this was nice.
Sorry for late answer but i wanted to test with and without json and i finally used Shannon solution :
$data = json_decode($_POST['myData'], true);
The second parameter return this output instead of Jason output :
array(1) { ["objectiflist"]=> array(2) { [0]=> array(2) { ["id"]=> int(1) ["label"]=> string(3) "one" } [1]=> array(2) { ["id"]=> int(2) ["label"]=> string(3) "two" } } }And then, to get my data :
$data['objectiflist'][0]['id'];
TY everybody for your help ;)
In my PHP (5.4) page I use the function file_get_contents to read post data, otherwise PHP don't see nothing.
$aRequest = json_decode(file_get_contents('php://input'), true);
Your Answer
Think you can help? Login to answer this question!