HELP: http.get does not get the json result from a php file


#1

I’ve been stuck on this for quite a long time now. I can’t figure out what’s wrong, and I am getting nowhere. I would really appreciate some advise!

Problem:

  • Instead of the json result (ex: {“a”:1,“b”:2,“c”:3}) I get the whole php file ( I know this because in the index.html file, I tested it with {{ items }} )
  • When I load the php file instead of the index.html, I see json though
  • The HTTP status of the php file is 302 also…
  • I’ve tried putting the Context-type: application/json in the php file and setting cache to false

app.js
.controller('EntryController', ["$scope", "$http", function($scope, $http) { $scope.items = []; $http({ method: 'GET', cache: false, url: '../services/getItemtest.php' }) .then(function(response) { $scope.items = response.data; }, function(response){ $scope.items = 'Could not retrieve Items'; }); ...

getItemtest.php
<?php header("Content-Type: application/json"); $result = array('a'=>1, 'b'=>2, 'c'=>3); echo json_encode($result); ?>


#2

The fact that you have a php page within your ionic app…makes me scared :fearful:. I mean, how does that even work?


#3

Sorry, I am new to this. Can you explain why it’s scary/wrong?


#4

For PHP to do anything, it needs to be fed to a PHP interpreter. Those tend to live on servers, not inside HTML5 applications.


#5

Okay, ionic was running live-reload server and I thought that was good enough, but from what you mentioned, I guess it doesn’t have the PHP interpreter.


#6

please use php as a web services with data as json format. why you want php page using http . If want use inappbrower cordova plugin .


#7

may be problem of json array. check it