0

I'm doing call from angularjs app to Rest web service. Most of the times request getting cancel. Sometimes first call it's getting cancel or sometimes second call getting cancel. And sometimes it's work fine.

code is:

$http({
    method : 'POST',
    url : 'https://hostname:8080/myapp/api/j/save-file',
    data : {fileName: 'abc.txt'}
}).success(function (data) {
    alert("success");
}).error(function (data) {
    alert("failed: ", data);
});

enter image description here

Anil Jagtap
  • 1,740
  • 4
  • 27
  • 44

2 Answers2

0
$http({
    method : 'POST',
    url : 'http://localhost:8080/myapp/api/j/save-file',
    data : {fileName: 'abc.txt'}
}).success(function (data) {
    alert("success");
}).error(function (data) {
    alert("failed: ", data);
});

remove https and place HTTP will resolve your issue.

:-) make sure you have called perfect API with the perfect route.

:-) if you are calling internal API then you can /myapp/api/j/save-file.

Parth Raval
  • 4,097
  • 3
  • 23
  • 36
  • Ravel, After I removed https then I'm getting status 301 code. – Anil Jagtap Apr 23 '19 at 07:47
  • I'm not calling internal api. I'm calling anothe web app service – Anil Jagtap Apr 23 '19 at 07:55
  • @AnilJagtap, Ok no problem, when you are calling `localhost` APIs then you need not add `https`. – Parth Raval Apr 23 '19 at 08:00
  • I updated my question. Instead of localhost I gave host name of web application. Can you please check it once? – Anil Jagtap Apr 23 '19 at 08:41
  • @AnilJagtap, As I said It will never work with `https` in my above comment and answer as well. if you deploy on the secure server then you can use `https`. https://stackoverflow.com/questions/2746047/why-not-use-https-for-everything https://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data these will help you to understand the concepts if you want to understand. first, try to understand how it works then implement this functionality. you will surely do it. :-) – Parth Raval Apr 23 '19 at 17:49
  • Switching from HTTPS to HTTP won't fix an AJAX request getting canceled. That just shows a complete lack of understanding of what the difference between HTTP and HTTPS is. Furthermore, if the web app service you're calling serves HTTP, it's not going to work at *all* if you do HTTPS. – Newms Apr 24 '19 at 22:45
  • And to clarify, the 301 HTTP status code makes sense; it's probably the API saying "Hey, you're trying to access this over HTTP; use HTTPS instead!". – Newms Apr 25 '19 at 01:16
0

Without knowing more about what's sending the request, the webserver you're sending it to, etc., we really can't diagnose this too much. However, I can offer the following advice:

  1. HTTP vs HTTPS won't make any difference here. If you're sending to a site that explicitly uses the protocol you're not using, you'll either get something like an HTTP 404 error, or a 301 (redirect) status.
  2. "Perfect" route also doesn't make a lick of sense. Of course, if you are trying to access a route at the wrong address, it won't work, but you'd still get a 404 (or 500, or whatever).
  3. Yes, you can use a relative URL for accessing stuff on the same server, but you can also use the full absolute URL. It makes no difference here, and does not explain at all why your request is getting cancelled.

Here's a test. What happens if you comment out every other 'thing' in your AngularJS app, leaving just the app definition itself, the controller definition, and try running it like that way? Does it still cancel? Try to find out at what point it's cancelling by breaking it down to the bare essentials.

If that still cancels, what about (as much as I hate using it) a jQuery $.post() request, to see if it's some issue specific with AngularJS and how you're using it?? Does the same error (with jQuery or AngularJS) happen in Firefox, Chrome, and any other browsers you have?

EDIT:

I've made a quick JS fiddle over at https://jsfiddle.net/cLu04r1t/1/ , using the same $http method you're using . Does your code throw the same error with this minimalist example (if you replace the URI I send it to with your example)?

Finally, take a look over at What does status=canceled for a resource mean in Chrome Developer Tools?. Could any of those be culprits behind why your AJAX request is getting cancelled?

Newms
  • 124
  • 1
  • 6
  • I'm not using JQuery. It's pre angular js app and I want to call third party web service url. Means my another web application web service url – Anil Jagtap Apr 24 '19 at 08:27
  • Sorry, lemme clarify. I meant trying just a regular HTTP request with something simple like jQuery's `$.post()` to see if there's some more general issue with your browser/setup by basically eliminating any "complexity". Just stick it on your page via a script tag or something. If jQuery works, and AngularJS doesn't/isn't, then you know there's some configuration issue with AngularJS. – Newms Apr 24 '19 at 22:42