简体   繁体   中英

AngularJS Post data to PHP - trying to get property of non-object

I've been trying to post data from AngularJS to a PHP script and I'm having a little problem.

$scope.submitRecipe = function () {
    $http({
        url: "../assets/php/scripts/create-recipe.php",
        method: "POST",
        data: {
            "recipeName" : $scope.recipeName
        }
    }).then(function (response) {
        alert(response.data);
    });
}

PHP Code:

<?php
$request = json_decode(file_get_contents("php://input"));

echo $request->recipeName;
?>

When I submit (call the function submitRecipe()) it does return the recipeName but it then continues to my PHP Script and I get the error:

Notice: Trying to get property of non-object in..

Any help and pointers are greatly appreciated.

My guess is that json_decode returned null , causing this line to throw the error.

echo $request->recipeName;

Check that the data return by

json_decode(file_get_contents("php://input"))

is infact not null and the Content-Type is application/json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM