NetworkError:无法在'XMLHttpRequest'上执行'send' [英] NetworkError: Failed to execute 'send' on 'XMLHttpRequest'

查看:4735
本文介绍了NetworkError:无法在'XMLHttpRequest'上执行'send'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:


错误:NetworkError:无法在'XMLHttpRequest'上执行'send':无法加载' 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?

编辑:我试过发送一个没有AJAX的正常POST请求,它也引发了一个'undefined'错误:

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) });
        });
});

我尝试过的其他事情:
1)将浏览器更改为Safari(同样的事情,服务器返回信息,但网站收到错误)
2)从false设置async = true。出于某种原因,当我将其设置为true时,服务器根本不响应。当它为假时服务器响应。

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.

推荐答案

我几分钟前就遇到了这个问题。问题是您需要将 async:false 设置为 async:true 。我不确定为什么这样可行,我想因为HTML5比XML更新。

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.

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

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..."

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

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

代码来源于此问题

这篇关于NetworkError:无法在'XMLHttpRequest'上执行'send'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆