REST + Angular = null object.

Status
Not open for further replies.
Primer: I'm suck at new stuff, so I'm working my way through it as best I can. I apologize when you notice I'm probably doing everything wrong.

Here I am, I've got an app that has a few REST endpoints that spit out some json. I know they work quite nicely because I can throw the uri in a browser and see the json that gets spit out. So I decided to try my hand at some Angular and see if I could consume said json. A quick and dirty test controller hits my endpoint, but the $http.then always hits the failure condition with a null response object. I've verified that my REST service is sending actual content, and as mentioned the same uri spits out the proper json in a browser address bar. I'm at a loss.

Code sample time:

Code:
    var app = angular.module('test', []);

        app.controller('testcontroller', function($scope, $http) {
            $scope.CallRest = function() {
                var testreq = encodeURIComponent(JSON.stringify({})); // add an empty json object to the uri
                var req = {
                    method: 'GET',
                    url: 'http://localhost:8088/restthing/endpoint?request=' + testreq
                }

                $scope.uri = req.url;

                $http(req).then(function (response) {
                    alert('Success');
                    $scope.json = angular.toJson(response.data);          
                    $scope.status = response.status;          
                }, function (response) {
                    alert('Failed');
                    $scope.json = angular.toJson(response.data);
                    $scope.status = response.status + ' ' + response.statusText;
                });
            }
        });

CallRest() is tied to a HTML button/ng-click, and then the response data is supposed to get spit out to a thingee on the page. No matter what I try I get a null object for response.data and a -1 for response.status. I'm at a loss.

Feel free to scoff. Angular and JS is completely not my cup of tea. I'm just trying something new here and even getting it to talk to the endpoint is a miracle imho. Appreciate any tips.
 

krimhorn

Ars Legatus Legionis
39,865
I'll double up on CanSpice's comment. Knowing precisely what the request (particularly the URI) and response looked like will help greatly.

The first thing that I'd do is make certain that the uri being sent is what I'd expect. Since you're relying on built-in JS functions I wouldn't expect a problem there but you never know. You could try relying on Angular's $http config object's 'params' parameter for the query string as well. IIRC, it will encode an object as well as taking strings.

Code:
var req = {
   method: 'GET',
   url: 'http://localhost:8088/restthing/endpoint',
   params: {
             request: {}
           }
}

That the response was actually null is odd. I'd expect, at most, the data to be null or undefined. The object itself is created by Angular to translate between the XMLHttpRequest and the promise.
 
With my original code, the uri is as expected:

Code:
req = Object {method: "GET", url: "http://localhost:8088/restthing/endpoint?request=%7B%7D"}

I am not seeing any response headers (using Chrome) from the actual $http request. Just the initial page load and angular namespace load. I know the call is being made as the rest call is hitting my rest service application (C/C++ based). I've traced all the way down to the rest service right before it sends back the response, I know there's a proper json payload being sent back. It just seems as if I'm not doing something right with $http.
 

Jonathon

Ars Legatus Legionis
17,645
Subscriptor
[url=http:/civis/viewtopic.php?p=30554031#p30554031:egv4td4o said:
SunnyD[/url]":egv4td4o]Found it...

Code:
XMLHttpRequest cannot load http://localhost:8088/restthing/endpoint?request=%7B%7D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

CORS related. Oops.
That was going to be my first guess-- Angular doesn't get anything intelligent in its error callback because the request doesn't get made at all (the browser does a preflight check then says NO YUO to the real request).

Always best to keep an eye on the Javascript console when building stuff with Angular's $http.
 
[url=http:/civis/viewtopic.php?p=30564399#p30564399:2cusohs4 said:
Jonathon[/url]":2cusohs4]
[url=http:/civis/viewtopic.php?p=30554031#p30554031:2cusohs4 said:
SunnyD[/url]":2cusohs4]Found it...

Code:
XMLHttpRequest cannot load http://localhost:8088/restthing/endpoint?request=%7B%7D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

CORS related. Oops.
That was going to be my first guess-- Angular doesn't get anything intelligent in its error callback because the request doesn't get made at all (the browser does a preflight check then says NO YUO to the real request).

Always best to keep an eye on the Javascript console when building stuff with Angular's $http.

Thing is the console wasn't giving this error. I wasn't getting an exception of any sort either. The only thing that was happening was that the response object was null and nothing was telling me why. I actually had to step down into several layers of Angular to see this error being thrown. Without stepping into it it was never getting shown in the console. (In Chrome at least)
 

Jonathon

Ars Legatus Legionis
17,645
Subscriptor
[url=http:/civis/viewtopic.php?p=30565691#p30565691:g69dk8xl said:
SunnyD[/url]":g69dk8xl]
[url=http:/civis/viewtopic.php?p=30564399#p30564399:g69dk8xl said:
Jonathon[/url]":g69dk8xl]
[url=http:/civis/viewtopic.php?p=30554031#p30554031:g69dk8xl said:
SunnyD[/url]":g69dk8xl]Found it...

Code:
XMLHttpRequest cannot load http://localhost:8088/restthing/endpoint?request=%7B%7D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

CORS related. Oops.
That was going to be my first guess-- Angular doesn't get anything intelligent in its error callback because the request doesn't get made at all (the browser does a preflight check then says NO YUO to the real request).

Always best to keep an eye on the Javascript console when building stuff with Angular's $http.

Thing is the console wasn't giving this error. I wasn't getting an exception of any sort either. The only thing that was happening was that the response object was null and nothing was telling me why. I actually had to step down into several layers of Angular to see this error being thrown. Without stepping into it it was never getting shown in the console. (In Chrome at least)
Odd-- I've always seen that particular error make its way up to the console (also working exclusively in Chrome).

Something else funky might be going on that's swallowing up errors-- what version of Angular are you on? Any third-party code (besides Angular) involved? ("Origin 'null'" in the error message also looks strange to me-- should be the host:port that you're running your development web server on.)
 
[url=http:/civis/viewtopic.php?p=30565801#p30565801:1d7f0ift said:
Jonathon[/url]":1d7f0ift]
[url=http:/civis/viewtopic.php?p=30565691#p30565691:1d7f0ift said:
SunnyD[/url]":1d7f0ift]
[url=http:/civis/viewtopic.php?p=30564399#p30564399:1d7f0ift said:
Jonathon[/url]":1d7f0ift]
[url=http:/civis/viewtopic.php?p=30554031#p30554031:1d7f0ift said:
SunnyD[/url]":1d7f0ift]Found it...

Code:
XMLHttpRequest cannot load http://localhost:8088/restthing/endpoint?request=%7B%7D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

CORS related. Oops.
That was going to be my first guess-- Angular doesn't get anything intelligent in its error callback because the request doesn't get made at all (the browser does a preflight check then says NO YUO to the real request).

Always best to keep an eye on the Javascript console when building stuff with Angular's $http.

Thing is the console wasn't giving this error. I wasn't getting an exception of any sort either. The only thing that was happening was that the response object was null and nothing was telling me why. I actually had to step down into several layers of Angular to see this error being thrown. Without stepping into it it was never getting shown in the console. (In Chrome at least)
Odd-- I've always seen that particular error make its way up to the console (also working exclusively in Chrome).

Something else funky might be going on that's swallowing up errors-- what version of Angular are you on? Any third-party code (besides Angular) involved? ("Origin 'null'" in the error message also looks strange to me-- should be the host:port that you're running your development web server on.)

Angular is just referencing 1.4.8 out of a static page from localhost. It was a quick and dirty test to see if the application serving the REST endpoints was working as expected and start getting a little bit familiar with Angular and JS, since our in-house UI toolkits are built around it. Nothing is actually hosted anywhere yet. I didn't at first expect there to be any cross-site issues with everything being "localhost", but I guess localhost and localhost:8088 mean cross-site to the browser. Or something. I h ad a hunch this might have been the case, but not getting the error right in my face the whole time was confusing me, and therefore I thought I was possibly doing something programmatically wrong.
 
Status
Not open for further replies.