Javascript向不同主机发送请求

Javascript向不同主机发送请求,javascript,post,xmlhttprequest,request,Javascript,Post,Xmlhttprequest,Request,我想通过JavaScript将POST请求发送到具有一些请求头和POST正文的不同主机 我该怎么做 我使用XMLHttpRequest进行了尝试,但出现以下错误: 0x80004005(NS\u错误\u故障) 除非您的浏览器支持CORS且远程主机允许您的源站(或允许所有源站(*)),否则XHR不可能。看 来自的此功能将让您知道: function createCORSRequest(method, url) { var xhr = new XMLHttpRequest(); if ("w

我想通过JavaScript将POST请求发送到具有一些请求头和POST正文的不同主机

我该怎么做

我使用XMLHttpRequest进行了尝试,但出现以下错误:
0x80004005(NS\u错误\u故障)


除非您的浏览器支持CORS且远程主机允许您的源站(或允许所有源站(*)),否则XHR不可能。看

来自的此功能将让您知道:

function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {

    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    // Otherwise, CORS is not supported by the browser.
    xhr = null;

  }
  return xhr;
}

var xhr = createCORSRequest('GET', url);
if (!xhr) {
  throw new Error('CORS not supported');
}
cd C:\Program Files(x86)\Google\Chrome\Application\ chrome.exe--允许从文件访问文件--禁用web安全 暂停


这允许您绕过google chrome中的同源策略并测试您的内容。

您不能,出于安全原因,这是禁止的,请看,您可以在任何地方使用from to POST…我使用的是ff22,因此它确实支持CORS。我如何确定远程主机是否允许origin?它正在工作。CORS是受支持的,但我没有得到任何回应。
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {

    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    // Otherwise, CORS is not supported by the browser.
    xhr = null;

  }
  return xhr;
}

var xhr = createCORSRequest('GET', url);
if (!xhr) {
  throw new Error('CORS not supported');
}