1

I am getting a valid JSON request but only $http fail method is invoked. It shows OK 200 in firebug. Also the view is not getting updated. Newbie trying out AngularJS so something could be too wrong :)

 $http.jsonp("http://localhost:51377/api/Books/1").success(function(data, status, headers, config) {
        alert("Suc");
        $scope.people = data;
    }).error(function(data, status, headers, config) {
        $scope.people = data;
        alert("fail");
    });         

This is the response in firebug:

{"ID":1,"BookName":"ASP.Net"}   

Also fails for:

[{"ID":1,"BookName":"ASP.Net"}] 

getting 404 in status code in $http call. I am using jsonp which should take care CORS? as I read from somewhere. My angular is in file:// protocol

Update-1: Tried with launching my angular in localhost from VS. Same results

Update-2: I added configuration changes to remove CORS in my backend .net webapi and now it works with the following code but still the original above code using jsonp fails

        $http({
        method: 'get',
        url: 'http://localhost:51377/api/Books/',
        responseType: "json"
    }).success(function (data, status, headers, config) {
        alert(data);
        console.log('Success: ' + data);
        $scope.people = data;
    }).error(function (data, status, headers, config) {
        console.log('Error: ' + data);
    });
3
  • did you tried angular file putting in http protocol? Commented Apr 21, 2016 at 4:28
  • Did you try adding ?callback=JSON_CALLBACK (see my answer below)? It's in the angular docs. Also, this JSFiddle shows that it is required: jsfiddle.net/saarmstrong/hYACX/8/light Commented Apr 21, 2016 at 13:33
  • @MarkWatson I will try that and update here. Commented Apr 27, 2016 at 4:25

2 Answers 2

0

I believe you need to add a callback parameter to your request:

$http.jsonp("http://localhost:51377/api/Books/1?callback=JSON_CALLBACK")

More info here:

https://docs.angularjs.org/api/ng/service/%24http#jsonp

Also, see this JSFiddle (not mine):

http://jsfiddle.net/saarmstrong/hYACX/8/light/

When you remove the callback parameter it fails...

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

Comments

0

try to use only "/api/Books/1" instead of "http://localhost:51377/api/Books/1"

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.