1

I have this code in AngularJS:

$http({
  url: my_url,
  method: "GET",
  data: null,
  headers: {
    "Content-Type": "application/json",
    "my-token": "mytoken",

  }
}).then(function(response, err) {
  console.log(response)
  console.log(err)
});

When the URL is correct and the status is 200, the response is displayed with status 200. But I want now to test with a wrong Url, then nothing is displayed, neither response nor error, so how to detect if there is no response?

4
  • 1
    there is always a error code or status Commented Nov 22, 2017 at 11:14
  • 1
    Use errorCallback of then() method i.e. .then(function (response, err) { }, function(response){ //do stuff} ); Commented Nov 22, 2017 at 11:15
  • refer to this url stackoverflow.com/questions/17080146/… Commented Nov 22, 2017 at 11:16
  • did you try to use all of these? stackoverflow.com/questions/23559341/… Commented Nov 22, 2017 at 11:17

4 Answers 4

3

By reading the $http documentation you can handle errors inside your error callback function. Also take a look at this HTTP Status Code list. Any 4xx status code e.g. 404 - not found will end inside the errorCallback function. You are also be able to handle the HTTP status by accessing response.status inside your callback functions.

Please note that there is always a response / HTTP Status code while performing an HTTP-Request.

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

>> Demo fiddle

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

7 Comments

Also there is no response displaying on successCallback and errorCallback when the url is wrong !
@user2285831 sure there is a response (404 - not found). Take a look at the demo fiddle. Please tell me the URL you try to access.
It's a url of a file in my server
@user2285831 open your browser debugger - in network tab what is your server responding when you try to access this file via $http?
this is only on network in the browser, but there is no response !
|
0

then takes two functions as parameters. First is called for success and Second is called for error. This is called promise.

Use following code to catch error:

.then(function(response) {
    // Success
}, function(error) {
    // Error
})

2 Comments

have displayed error in console? What kind of URL you are using?
it's a url of a file in my server
0
$http({
    url: my_url,
    method: "GET",
    headers: {
        "Content-Type": "application/json",
        "my-token": "mytoken",

    }
}).then(function(response) {
    console.log(response)
}, function(error) {
    console.log(error);
    console.log(error.data);
});

Add a function as second parameter to .then() will track error cases for http calls.

1 Comment

can you try by removing data:null
-1

The problem was with this line, it should be removed "my-token": "mytoken", Thank you !

1 Comment

This answer is wrong. This is nothing depending on your headers as you can see here: jsfiddle.net/ca074sgm You will get a -1 HTTP-Status once a timeout hit in. There is ALWAYS a response.

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.