Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
If I consume a service by paste the link, e.g. https://myURL/myService?Address=XXXX&f=json on a browser, the results display well. In my JavaScript script,
var xhReq = new XMLHttpRequest();
  var url2 = "https://myURL/myService?Address=XXXX&f=json";
  xhReq.open("GET", url2, false);
  xhReq.responseType = 'json';
  try {
      xhReq.send(null);
      var serverResponse = xhReq.responseText;
  } catch (exception) {
      var _status = xhReq.status;    // _status value is 0  no response
      if (exception instanceof NetworkError) {
          console.log('There was a network error.');
      }
  }

I got Network error. What is the possible reason for the Network error? Thanks if you can hint me.

What I have tried:

Network Error on a service
in a AJAX call
Posted
Updated 11-Aug-21 5:35am
v2

1 solution

You really haven't provided enough context to really know what the issue is with your request, you need to look at the exception variable and look at what information it's providing you. Have you tried debugging this Javascript in the browser, stepping through the code line-by-line to see what's happening?

That being said, if you're trying to issue an AJAX request to another website you need to be aware of cross-origin (CORS)[^] restrictions that websites may put in place. The website may choose to prevent external websites from making these sorts of requests, and your browser will honour that decision. You would receive errors when attempting to make API requests as the server rejects them.

If you own the API yourself (for example, running it on your local machine or have access to wherever the server is being hosted, and/or the source code of the application) you may need to do some additional configuration to allow external connections.

Enable Cross-Origin Requests (CORS) in ASP.NET Core | Microsoft Docs[^]
Getting Started | Enabling Cross Origin Requests for a RESTful Web Service[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900