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?

lin
  • 17,956
  • 4
  • 59
  • 83
user2285831
  • 419
  • 1
  • 5
  • 18
  • 1
    there is always a error code or status – Niklesh Raut Nov 22 '17 at 11:14
  • 1
    Use `errorCallback` of `then()` method i.e. `.then(function (response, err) { }, function(response){ //do stuff} );` – Satpal Nov 22 '17 at 11:15
  • **refer to this url** https://stackoverflow.com/questions/17080146/error-handling-in-angularjs-http-get-then-construct – Akshay Nov 22 '17 at 11:16
  • did you try to use all of these? https://stackoverflow.com/questions/23559341/using-success-error-finally-catch-with-promises-in-angularjs – plvice Nov 22 '17 at 11:17

4 Answers4

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

lin
  • 17,956
  • 4
  • 59
  • 83
  • Also there is no response displaying on successCallback and errorCallback when the url is wrong ! – user2285831 Nov 22 '17 at 11:25
  • 1
    @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. – lin Nov 22 '17 at 11:26
  • It's a url of a file in my server – user2285831 Nov 22 '17 at 11:36
  • @user2285831 open your browser debugger - in network tab what is your server responding when you try to access this file via `$http`? – lin Nov 22 '17 at 11:39
  • yes with a wrong url i have this Request URL:https://www.myurlwrong.com Referrer Policy:no-referrer-when-downgrade – user2285831 Nov 22 '17 at 11:51
  • `Referrer Policy` is an response header. In that way you actually recive a response. This request also should result in an error callback with status code `-1`. – lin Nov 22 '17 at 11:55
  • 1
    this is only on network in the browser, but there is no response ! – user2285831 Nov 22 '17 at 13:35
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
})
Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25
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.

Nikhil Mohanan
  • 1,260
  • 1
  • 12
  • 23
-1

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

user2285831
  • 419
  • 1
  • 5
  • 18
  • 1
    **This answer is wrong**. This is nothing depending on your headers as you can see here: http://jsfiddle.net/ca074sgm/ You will get a **-1** HTTP-Status once a timeout hit in. There is **ALWAYS a response**. – lin Nov 23 '17 at 06:43