How can I handle a failed XMLHttpRequest?

I’m using Ionic 1.0.0-beta.8
Inside of the anonymous function inside of createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument), I get an error at
xhr.send(post || null); //Line 17399 of ionic.bundle.js
The error says, “GET https://[the address I’m hitting]”

I’m sure that this error occurs inside of xhr.onreadystatechange callback somwhere. I want to know exactly where it’s writing the error.

I have tried the following steps with no success…

  • $exceptionHandler: I created my own $exceptionHandler and it never gets called. This makes me assume that the exception is being handled somewhere.
  • Using my $resource error callback: Like this… $resource(apiUrl).get(null, successCallback, failureCallback). The FailureCallback IS called before I get the error.
  • Using the $promise from the $resource.Get call: This also gets called before the error.

I dont know if i’ve understood well your post.
But to get the content of an external resource, inject $http and use it to get this way:

$http({method: 'GET', url: 'http://www.yourpage.com'}).
	    success(function(data, status, headers, config) {
	      console.log("+ JSON: server");
	      console.log(data);
	      $scope.server = data;
	    }).
	    error(function(data, status, headers, config) {
	      // called asynchronously if an error occurs
	      // or server returns response with an error status.
	    });

In case you need to post info to your server, you can send the data using the shortcut method this way:

$http.post('/someUrl', data).success(successCallback).error(errorCallback);

More info in the angularjs docs: https://docs.angularjs.org/api/ng/service/$http#post

1 Like

Thank you for your response. I used $http instead of $response and got the same error in the console…
GET http://www.mypage.com/api/FieldOffices

If I could figure out where this was coming from, I would be so happy.

mmm. i tried to open your URL http://www.mypage.com/api/FieldOffices and doesn’t look like a JSON response.

Try to edit your response in your backend to receive a valid JSON object. Maybe it’s that.
Try to test with other urls and get other content.

I think regardless if it is valid JSOn or not a successful request to the page should trigger the success callback I might be wrong.

Sometimes i’ve experiencied a javascript error on my $http requests due to an html response from my backend (usually given by some error in my backend)