NetworkError: Failed to execute 'send' on 'XMLHttpRequest'

江枫思渺然 提交于 2019-11-29 17:05:36

问题


I'm trying to do a POST ajax request to a server hosted locally on my laptop but I can't seem to get any information back. When I click a button on my site (localhost), I can see the server passing back the correct information but on the front end I get this error:

error: NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://comp-ip'.

var param = JSON.stringify({varA:"varA",varB:"varB"});

$.ajax({
  type: "POST", 
  url: "http://comp-ip",
  async: false, 
  data: param,
  success: function(result, status, xhr){
    alert(result + ": " + status);
  },
  error: function(xhr, status, err) {
    alert(status + ": " + err);
  }
});

It seems to be triggering an error every time and not 'success'. Anyone have any idea what is wrong?

Edit: I've tried sending a normal POST request without AJAX and it throws me an 'undefined' error as well:

$(document).ready(function(){
    var param = JSON.stringify({varA:"varA",varB:"varB"});
     $("#btn").click(function(event){
           $.post( 
              "http://ip",
               param,
               function(data) {
                 $('#container').html(data);
               }
           ).fail(function(error) { alert(error.responseJSON) });
        });
});

Other things I've tried: 1) Changing browsers to Safari (same thing, server returns information but the site gets an error) 2) Setting async = true from false. For some reason when I set it to true, the server doesn't respond at all. When it's false the server responds.


回答1:


I had this problem literally a few minutes ago. The problem is that you need to set async:false to async:true. I'm not sure exactly why this works, I guess because HTML5 is newer than XML.

Error is a bit different on this site but I think it's similar: JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..."

Update

Hi, I'm back with some new and improved information. The Cross-Origin occurs with ports as well as domains/IPs, and will probably be in place with most decent browsers. If you want to know what is happening, try changing the IP to localhost or vice versa (if you are using localhost). Keep in mind this issue can occur when you are using different ports as well. For a quick fix, make sure that in the headers from whatever the back-end server is that you are putting out the correct Access-Control header response.addHeader("Access-Control-Allow-Origin", "*");.

Code is derived from this question




回答2:


It was a Cross Origin Resource problem (CORS). My server was returning the correct information to me but my browser refused to accept it. To fix it I had to add 2 lines on my java server (not my site) to set them as the response header:

yourownvariable.setHeader("Access-Control-Allow-Origin:", "origin url of your site");
yourownvariable.setHeader("Access-Control-Allow-Methods", "GET, POST,PUT");



回答3:


I was having the same problem, with an identical error message. In my case it was caused by the server setting a Feature-Policy header with sync-xhr: none. Removing this directive fixed the problem.




回答4:


You can test your webpages with Chrome on Windows like this

chrome.exe --allow-file-access-from-files


来源:https://stackoverflow.com/questions/32878613/networkerror-failed-to-execute-send-on-xmlhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!