jQuery NetworkError:无法在“XMLHttpRequest”上执行“发送”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32878613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:55:35  来源:igfitidea点击:

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

jqueryajaxhttp-post

提问by Moo33

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:

我正在尝试向笔记本电脑本地托管的服务器执行 POST ajax 请求,但我似乎无法获取任何信息。当我单击我的站点 (localhost) 上的按钮时,我可以看到服务器传回正确的信息,但在前端我收到此错误:

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

错误:网络错误:无法在“XMLHttpRequest”上执行“发送”:无法加载“ 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:

编辑:我尝试发送一个没有 AJAX 的普通 POST 请求,它也抛出了一个“未定义”错误:

$(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)将浏览器更改为 Safari(同样的事情,服务器返回信息但站点收到错误)2)从 false 设置 async = true。出于某种原因,当我将其设置为 true 时,服务器根本没有响应。当它为假时,服务器响应。

回答by ytpillai

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

几分钟前我确实遇到了这个问题。问题是你需要设置async:falseasync:true. 我不确定为什么会这样,我猜是因为 HTML5 比 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..."

这个站点上的错误有点不同,但我认为它是相似的:JavaScript console.log 导致错误:“主线程上的同步 XMLHttpRequest 已弃用......”

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", "*");.

嗨,我回来了一些新的和改进的信息。Cross-Origin 发生在端口以及域/IP 中,并且可能适用于大多数不错的浏览器。如果您想知道发生了什么,请尝试将 IP 更改为 localhost,反之亦然(如果您使用的是 localhost)。请记住,当您使用不同的端口时也会出现此问题。要快速修复,请确保在来自后端服务器的任何标头中都放置了正确的 Access-Control 标头response.addHeader("Access-Control-Allow-Origin", "*");

Code is derived from this question

代码来自这个问题

回答by Moo33

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:

这是一个跨源资源问题(CORS)。我的服务器正在向我返回正确的信息,但我的浏览器拒绝接受它。要修复它,我必须在我的 Java 服务器(不是我的站点)上添加 2 行以将它们设置为响应标头

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

回答by Mark Fisher

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

我遇到了同样的问题,有相同的错误消息。在我的情况下,它是由服务器设置Feature-Policy标头引起的sync-xhr: none。删除此指令解决了问题。

回答by Daniel Georgiev

You can test your webpages with Chrome on Windows like this

您可以像这样在 Windows 上使用 Chrome 测试您的网页

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