0

I'm getting the following error while requesting get method in Angular js

 XMLHttpRequest cannot load http://localhost:29527/Services/ProfileService.svc/ChildList?ParentId=37. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. 

This is my code

app.controller("AppCtrl", function($http) {
    var app = this;
    var config = {
        headers: {
            'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
            'Accept': 'application/json;'

        }
    };

    $http.get(childList,config)
        .success(function(data) {
            var carsFromServer = JSON.parse(data);
            console.log(data.getChildrenResult);
        });
});
0

3 Answers 3

2

Short story

In the beginning websites could load data from another websites. But soon people understood that it's very dangerous, as hackers could steal cookies from other sites. So CORS(Cross-Origin Resource Sharing) standard was introduced, it doesn't allow websites to load data from different domains. But still it can be configured.

Your question

Your url http://localhost:29527/Services/ProfileService.svc/ChildList?ParentId=37 seems a wcf service, which probably is self hosted, but you cannot use it for cross-domain-origin requests, until you implement such functionality as described here and host wcf in webserver. Enabling CORS in wcf sometimes brings lots of troubles, as wcf is very limited by default, even not possible in some conditions. Just because wcf doesn't understand http requests, and cuts a lot of header information. link provided before should temporarily solve, but...!

Recommendation

Make WebApi site with service references to your wcf service, add everything you need( routes, controllers, actions) and implement CORS there as it's done by applying attributes to the controllers(very easy). So you will end up with 3 projects (wcf service, asp.net webapi, angularjs website). Or better use angularjs backed as webservice of wcf. But having separate webapi server will be flexible, as you might want to make mobile versions, or just mobile native apps, or whatever you want. It will serve everything
hope helps. sorry for english

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

Comments

0

It's because http://localhost:29527/Services/ProfileService.svc/ChildList?ParentId=37 Has no Access-Control-Allow-Origin header present. This causes XMLHttpRequest to fail, since origin http://localhost:63342 (where your html is) is not allowed to read data from another origin.

Have you actually looked at the error message?

4 Comments

I tried with this headers:{'Access-Control-Allow-Origin': '*'} but it didn't make any changes
Raghu, did you try setting 'Access-Control-Allow-Origin': '*' on your localhost server which is serving up your API? That's the side of the coin which you need to be working with. The accepting server needs to allow cross origin resource sharing.
not really surprising. Read the message again - it is about the resource, not the request.
please, read up on same origin policy. By doing this the wrong way, you might introduce a MAJOR security hole.
0

Try with $http.jsonp instead of $http.get check out https://docs.angularjs.org/api/ng/service/$http#jsonp

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.