0

In my mobile, angular app i try to send a JSON object to the server for further processing.

This is my JSON

$sessionsString = {"ojoijoj":[{"station":"AABB","height":80,"period":"20","gsh":"1.11","gsp":"20","gsd":"225","wsh":"1.11","wsp":"5.3","wsd":"270","dt":"2015-07-08T00:00:00.000Z","station2":"9410230","windStrength":"3.6","windDirection":"220","tideHeightInCM":115.06811989101},{"station":"CCBB","height":90,"period":"10","gsh":"1.7","gsp":"10","gsd":"207.5","wsh":"2.7","wsp":"9.9","wsd":"207.5","dt":"2015-07-16T21:00:00.000Z","station":"94102302","windStrength":"3.17","windDirection":"308.502","tideHeightInCM":83.986622073579}],"y4":[{"station":"DD","height":90,"period":"15","gsh":"1.11","gsp":"15","gsd":"225","wsh":"2.3","wsp":"5","wsd":"297.5","dt":"2015-07-04T19:00:00.000Z","station2":"9410230","windStrength":"1.5","windDirection":"280","tideHeightInCM":134.37293729373}]};`

I'm sending my JSON via

$http.post(requestURL, {'sessions':JSON.stringify(sessions)}).
  success(function(data, status, headers, config) {
      console.log('Success');
  }).
  error(function(data, status, headers, config) {
  }); 

and figured through some other references that I have to get the data through

$params = json_decode(file_get_contents('php://input'), true);

to retrieve the parameters rather than with regular $_POST for some reason. (Found this in other posts, $_POST didnt work -> is this thread safe by the way?)

However, I want to parse the data now, and it doesn't work.

$sessionsString = $params['sessions'];
$sessions = json_decode($sessionsString,true);
$spotNames = array_keys($sessions); 

array_keys fails with no keys found. When I directly assign the above JSON string to $sessionString it works fine. What's happening? I dont know how to debug this because it's a POST parameter that I can't fully see and the file read out in PHP could be a problem, too.

Thanks, EL

2 Answers 2

2

Dont stringify in $http just pass in the object. angular will take care of converting to json

$http.post(requestURL, {sessions:sessions})...

Also the first json_decode should now work without needing the second

Sign up to request clarification or add additional context in comments.

4 Comments

Interesting. I am checking the apache access/error logs and it reports that it recieves two requests. One without parameters and one with all parameters. I am running only one request in the whole app. How could that be?
is this a cross domain request? Even a different port counts as cross domain. If so, will have an OPTIONS preflight request also
Does it require a specific answer on the preflight request or is it really just a ping? How shall I deal with that in PHP?
logs are probably counting the OPTIONS preflight then. It's the preflight that needs the CORS headers or post won't get sent
0

Looks to me like you are calling json_decode twice. Not sure why $_POST didn't work for you, but you can always use var_dump($params)to see whats happening.

5 Comments

$_POST doesn't work because angular sends as application/json by default not form encoding unless you change request header
I see. That makes sense.
Is there a way to adapt $_POST to deal with the header in a more streamlined way?
Well the Content-Type of your post request should be application/x-www-form-urlencoded. Using angular I believe you can do the following $http({method:'POST', url:requestURL, headers:{'Content-Type':'application/x-www-form-urlencoded'}, data:'sessions='+JSON.stringify(sessions)}).success(...). Here's a reference. I believe this should allow you to use $_POST.
either way works fine. If it's sent in body as json you use what OP is using file_get_contents()

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.