Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I've been sending and requesting data from PHP files using Angular's $http service on my local server (XAMPP), but recently ran into multiple problems when I began doing it on my web server, hosted publicly. The first problem was that $http.post kept giving me this error:

Could not find www.websitepath.com/Object%20Object

Obviously not the URL I entered. I fixed this by using the $http service and just specifying its POST type. I know for a fact the scripts are connecting (as in the AngularJS script is reaching the PHP script) and the PHP script is working perfectly fine (I loaded it myself and got the string I wanted). However, AngularJS keeps giving me this:

Object {data: "", status: 200, headers: function, config: Object, statusText: "OK"}

The data seems to be empty despite the fact the PHP script is printing out a JSON string. The code for my AngularJS script and PHP script is below:

var promise = $http({
    method: "post",
    url: fetchTable,
    data: {
        choice: 2
    },
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
promise.then(function(response) {
    $scope.Units = response.data;
    $scope.errorMessage = 'Success!';
    console.log($scope.Units);
    console.log(response);
},function(error) {
    $scope.errorMessage = 'There was a problem fetching information';
});
}]);

And this is the PHP script I use to fetch my data:

    $request = file_get_contents('php://input');
    $data = json_decode($request);
    $choice = $data->choicePara;
    $query = "SELECT unit_id, unitname, unitlink, unitimg, hp, atk, def, rec, upvotes, downvotes, ranking, tier FROM vote_float_v4 ORDER BY ranking ASC";
    $result = mysqli_query($dbc, $query);
    if (!$query) {
        http_response_code(400);
        die();
    }
    else {
        $units_array = array();
        while ($unit_row = mysqli_fetch_array($result)) {
            $unit = new Unit($unit_row); //Create a new unit object based on the information from MySQL
            array_push($units_array, $unit);
        }
        print_r(json_encode($units_array));
    }

The PHP is printing exactly what I want and the AngularJS script is not indicating any errors it cannot find the PHP script. Actually, before when I had an error in my PHP syntax, AngularJS returned back the warning message to me. But for some reason, I keep getting this weird JSON object back as my response:

Object {data: "", status: 200, headers: function, config: Object, statusText: "OK"}

Why is the data empty?!?! How can I make AngularJS fetch the JSON data? Is there a problem with the way I'm returning it?

share|improve this question
    
edit, wrong, see below im no angular expert but the docs say you should use the .success callback of the promise docs.angularjs.org/api/ng/service/$http (not .then) – Plato Apr 6 '15 at 22:08
    
nvm I see the note about invoking it like you are. my only other guess is you aren't setting "Content-Type: application/json" header in php? – Plato Apr 6 '15 at 22:09
    
or possibly the php output is not actually making it to the http response. can you show the server-side code, how you invoke the php url? – Plato Apr 6 '15 at 22:11
    
Also, I don't see $dbc defined in php, did you remove this for security? And, you are die()ing if $query is falsy after sending the db request. Maybe you want to check if $result is falsy? – Plato Apr 6 '15 at 22:14
    
Actually, your first guess (Content-Type: application/json) worked!! I'll remember that from now on, I didn't know it was necessary. Thanks a lot!! – Philip Tsang Apr 6 '15 at 22:15
up vote 1 down vote accepted

copying from comment:

Try setting "Content-Type: application/json" header in php

Sadly I don't know exactly what this is fixing...

Can any angular wizards offer an explanation of why a response body without a Content-Type header is passed to the $http success callback as an empty string, instead of a json string?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.