0

I am learning from this site.

I use this for an Android application to fetch the PHP data and show the application.

When I start the web service in AngularJS, Google Chrome gives this error in the console:

XMLHttpRequest cannot load http://192.168.1.10/android_connect/JsonReturn.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

This is my JavaScript code:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>function GetUsers($scope, $http) {
    // this is where the JSON from api.php is consumed
    $http.get('http://192.168.1.10/android_connect/JsonReturn.php').
        success(function(data) {
            // here the data from the api is assigned to a variable named users
            $scope.users = data;
        });
}</script>

Then I set the HTML code:

<div ng-controller="GetUsers">

<table>
<thead><tr><th>ID</th><th>Name</th><th>Email</th></tr></thead>
<tbody>
<tr ng-repeat="user in users"><td>{{user.id}}</td><td>{{ user.name }}</td><td>{{user.email}}</td></tr>
</tbody>
</tfoot></tfoot>
</table>

</div>

I tried all the possible solutions posted, but it doesn't seem to fix the issue in my code.

2
  • I think the error is pretty much self explanatory No 'Access-Control-Allow-Origin' header is present on the requested resource. Access-Control-Allow-Origin should be in your header Commented Jan 17, 2015 at 14:27
  • i am add this but not working Commented Jan 18, 2015 at 18:28

2 Answers 2

3

You are trying to make a cross domain request using XMLHttpRequest. For that to work, the web server responding to the request needs to have the Access-Control-Allow-Origin response header set to *.

Sign up to request clarification or add additional context in comments.

1 Comment

Add this header('Access-Control-Allow-Origin: *'); in your JsonReturn.php
0

Alternatively, serve the Angular page from the same server than hosting your API. Then you don't have a cross-domain request.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.