javascript - AngularJS Post Request not catching JSON data return from PHP -
javascript - AngularJS Post Request not catching JSON data return from PHP -
i've been sending , requesting info php files using angular's $http service on local server (xampp), ran multiple problems when began doing on web server, hosted publicly. first problem $http.post kept giving me error:
could not find www.websitepath.com/object%20object
obviously not url entered. fixed using $http service , specifying post type. know fact scripts connecting (as in angularjs script reaching php script) , php script working fine (i loaded myself , got string wanted). however, angularjs keeps giving me this:
object {data: "", status: 200, headers: function, config: object, statustext: "ok"}
the info seems empty despite fact php script printing out json string. code angularjs script , php script 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 problem fetching information'; }); }]);
and php script utilize fetch 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 vote_float_v4 order 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 new unit object based on info mysql array_push($units_array, $unit); } print_r(json_encode($units_array)); }
the php printing want , angularjs script not indicating errors cannot find php script. actually, before when had error in php syntax, angularjs returned warning message me. reason, maintain getting weird json object response:
object {data: "", status: 200, headers: function, config: object, statustext: "ok"}
why info empty?!?! how can create angularjs fetch json data? there problem way i'm returning it?
copying comment:
try setting "content-type: application/json"
header in php
sadly don't know fixing...
can angular wizards offer explanation of why response body without content-type
header passed $http
success callback empty string, instead of json string?
javascript php json angularjs
Comments
Post a Comment