0

I can't understand why the data object seems doesn't exist at php server side page.

Angular

$http.post("serverside.php", {"data1":"okpass"}).success(function(data, status, header, config){
                console.log(data);// 
                console.log(status);//202 
                console.log(header);//function (c){a||(a=nc(b));return c?a[O(c)]||null:a}
    });

PHP

if( isset($_POST["data1"]) && $_POST["data1"] == "okpass" ){
    echo "It works!";
    exit(0);
}

2 Answers 2

1

Angulars http.post does not send form data, so you cant rely on phps $_POST. It sends json data. You can get the data like so:

$request = json_decode(file_get_contents("php://input"), true);
if( isset($request["data1"]) && $request["data1"] == "okpass" ){
    echo "It works!";
    exit(0);
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Donovant What doesnot work? What do you get if you debug $request - eg print_r($request);
echo $data["data1"] == "okpass"; Fatal error: Cannot use object of type stdClass as array in C:\EasyPHP-5.3.5.0\www\AngularJS\2014-MAR\$http\serverside.php on line 33
Ok the problem was insted put TRUE at the end off json_decode(), so now is $data = json_decode(file_get_contents("php://input"), true); for treating $data as array.
@Donovant Yes, should have spotted that. Will amend my answer - glad you got it working
0

In the PHP code, convert the JSON into good old $_POST like this:

//handles JSON posted arguments and stuffs them into $_POST
//angular's $http makes JSON posts (not normal "form encoded")
$content_type_args = explode(';', $_SERVER['CONTENT_TYPE']); //parse content_type string
if ($content_type_args[0] == 'application/json')
  $_POST = json_decode(file_get_contents('php://input'),true);

//now continue to reference $_POST vars as usual

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.