0

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?

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • It seems similar to your problem http://stackoverflow.com/questions/15485354/angular-http-post-to-php-and-undefined – SiZE Jul 15 '15 at 09:08
  • This answer http://stackoverflow.com/a/8893792/2967875 explain clearly why you get $_POST empty array. – zzas11 Jul 15 '15 at 09:17

2 Answers2

1

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
})
StudioTime
  • 22,603
  • 38
  • 120
  • 207
0

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;
    }];
});
Sjon
  • 4,989
  • 6
  • 28
  • 46