简体   繁体   中英

Sending $http.post data to PHP - Trying to get property of non-object in

I'm trying to do a simple ajax call for a search, yet php is throwing error (notice):

Trying to get property of non-object in.. etc etc

This is the Angular http call:

console.log($scope.query); // this works fine and logs correctly

$http({
  method: 'POST',
  url: url,
  //data: "searchTerm=" + $scope.query, // <-- also tried but failed
  headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}, {searchTerm: $scope.query})

also tried {"searchTerm": $scope.query}

PHP

$data = json_decode(file_get_contents("php://input"));
$searchTerm = $data->searchTerm;

Trying print_r($_POST); in the PHP file gives an empty array Array()

Is there a better way to pass the data between the two?

Found the answer after trying every combination known to man!

Hopefully this might help someone in the future if they have same issue...

PHP as described above...

ANGULAR

data = {
  'searchTerm' : $scope.query
};

$http({
  method: 'POST',
  url: url,
  data: data
})

Maybe you can config, and ajax(data:{param:'test'}) . And then in .php user $_POST['param'] .

app.config(function($httpProvider) {
    $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

    // Override $http service's default transformRequest
    $httpProvider.defaults.transformRequest = [function(data) {
        /**
         * The workhorse; converts an object to x-www-form-urlencoded serialization.
         * @param {Object} obj
         * @return {String}
         */
        var param = function(obj) {
            var query = '';
            var name, value, fullSubName, subName, subValue, innerObj, i;

            for (name in obj) {
                value = obj[name];

                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value instanceof Object) {
                    for (subName in value) {
                        subValue = value[subName];
                        fullSubName = name + '[' + subName + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value !== undefined && value !== null) {
                    query += encodeURIComponent(name) + '='
                            + encodeURIComponent(value) + '&';
                }
            }

            return query.length ? query.substr(0, query.length - 1) : query;
        };

        return angular.isObject(data) && String(data) !== '[object File]'
                ? param(data)
                : data;
    }];
});

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