2

In my angular application, I am trying to sent the data to php script and then return a callback.

Here is the angular code:

var data = { value: 'somestring' };

$http.post("post_backend.php", data).success(function(data, status) {
    alert(data + status);
}).error(function(data, status) {
    alert(data + status);
});

In my php code I have a simple calling back function:

if (isset($_POST['value'])) {
    header('Content-Type: application/json');
    echo json_encode('successful callback');
    exit();
}

But when it executes, I get a success status in the alert message and nothing else. I dont get the data ('successful callback') and can't figure out for about a couple of hours, what is the problem?

Mr_Pouet
  • 4,061
  • 8
  • 36
  • 47
MyOwnFan
  • 117
  • 1
  • 7
  • Looks like the `if (isset($_POST['value']))` condition is never met. What if you `echo($_POST)` ? What does PHP receive? – Jeremy Thille Jan 01 '16 at 17:52
  • @JeremyThille You are right, the condition is not met. But what is the problem? How can I fix it? – MyOwnFan Jan 01 '16 at 17:54
  • Don't use `alert()` instead use `console.log()` and open your dev tools in the browser, this should prevent debug data from being malformed. Also don't use the `+` operator, instead use a comma to seperate the arguments like so: `console.log(data, status)`. Make sure there are no error in your console and check the network tab to see if the request is firing correctly. – Jon Koops Jan 01 '16 at 17:57
  • @MyOwnFan `What is the problem? How can I fix it?` Well that's what we're trying to find out here. Hence my question "What if you `echo($_POST)` ? What does PHP receive?" That would be helpful if you answered :) – Jeremy Thille Jan 01 '16 at 18:00
  • @JeremyThille It returns an empty string. As I have said, you were right and the if statement is not triggered. – MyOwnFan Jan 01 '16 at 18:03
  • @MyOwnFan Are you sure the alert is executed from success? I think because the JSON wasn't parsed correct, angular will throw this to error handler. In this case it is a correct behavior. – Dmitri Pavlutin Jan 01 '16 at 18:10
  • You send it an object `{ value: 'somestring' }` and it gets an empty string?? O_o Something's definitely wrong here, but I don't know for sure what it is – Jeremy Thille Jan 01 '16 at 18:10
  • @JeremyThille For some reason it does not recognize this object properly. It does not detect the word value... – MyOwnFan Jan 01 '16 at 18:12
  • Problem is, PHP doesn't detect anything at all. You send it an object, and it sees an empty string... (----> Flies back to NodeJS :) – Jeremy Thille Jan 01 '16 at 18:15

3 Answers3

1

This code works for me.

$params = json_decode(trim(file_get_contents('php://input')), true);
if (isset($params['value'])) {
    header('Content-Type: application/json');
    echo json_encode(['status'=>'successful callback']);
    exit();
}
Viktor
  • 53
  • 1
  • 4
  • 1
    There should be a way simpler and safer then geting the contents of the file. It is insecure and there is need to specify, which file to recieve an input from. PHP should work as a service... – MyOwnFan Jan 01 '16 at 18:09
1

I found the answer. I had to reconfigure the way I have sent the data to php script. I found this very usefull article: How can I post data as form data instead of a request payload?

I had to implement jquery and add the header to my request. In the php script I dont need anything except for the if statement and echo.

Community
  • 1
  • 1
MyOwnFan
  • 117
  • 1
  • 7
0

Try put to responce a correct JSON, for example echo json_encode(['status'=>'successful callback']);

Viktor
  • 53
  • 1
  • 4
  • 2
    This is not an answer to the problem, just a suggestion. Should be put as comment. – Jeremy Thille Jan 01 '16 at 17:58
  • 1
    Did you add $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; to your application ? PHP doesn't work correct with "Content-Type: application/json" – Viktor Jan 01 '16 at 18:01
  • @Viktor How can I do that? – MyOwnFan Jan 01 '16 at 18:01
  • 1
    When i made my AngularJS + PHP app i used this article http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/ – Viktor Jan 01 '16 at 18:19