I have a php script to return json through GET, the code is as below
<?php
$output = //generate json data;
header('Content-type: application/json');
echo json_encode($output); ?>
I host this php in local host, so when i access http://localhost/test.php , i am able to see json data correctly.
Then I have a page using AngularJS, like below
<h1>{{myWelcome}}</h1>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http({
method : "GET",
url : "http://localhost/test.php"
}).then(function mySucces(response) {
$scope.myWelcome = response.data;
alert("success");
}, function myError(response) {
$scope.myWelcome = response.statusText;
alert("fail");
});
});
I run the page, nothing displayed in the UI. I didn't get any alert. Which means it's neither success nor fail. If I switch the URL to be other api. for example
url : "http://congress.api.sunlightfoundation.com/legislators?apikey=xxx"
I can get response correctly. It means my js code is good. So my question is, why i can't get json response from my localhost php. Is there anything wrong on my php, or it's my js's problem?
I tried to search in stackoverflow, some people suggest this but it didn't help.
$http.get('http://localhost/angmysql/api.php').success(function(response) {
$scope.users = response.data;
});
Thanks for the comments from @jay. Look at console, Here is the thing.
XMLHttpRequest cannot load http://127.0.0.1/test.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.