javascript AJAX 调用错误:错误:网络错误:DOM 异常 19

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17921822/
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-10-27 10:06:33  来源:igfitidea点击:

Error on AJAX call:Error: NetworkError: DOM Exception 19

javascriptajaxcross-domainjsonp

提问by Waseem

Got this error while making an cross domain ajax request.

进行跨域ajax请求时出现此错误。

Error: NetworkError: DOM Exception 19

错误:网络错误:DOM 异常 19

Please help on this . Stuck real bad :(

请帮忙解决这个问题。卡住真的很糟糕:(

回答by Grav

I got this error when using synchronous requests in Chrome:

在 Chrome 中使用同步请求时出现此错误:

var req = new XMLHttpRequest();
req.open("GET", url, false) // false denotes synchronous call
req.send();

Doing it asynchronous (which I wanted to anyways) didn't give me the error.

异步执行(无论如何我都想这样做)并没有给我错误。

I then discovered that the server rewrote the URL. If I used the rewritten URL, I didn't get the error. Probably a Chrome bug, since Safari handles the synchronous, non-rewritten case just fine.

然后我发现服务器重写了 URL。如果我使用重写的 URL,则不会出现错误。可能是 Chrome 的错误,因为 Safari 可以很好地处理同步的、非重写的情况。

Don't know if I'll report it, since synchronous XMLHttpRequests are deprecated anyways.

不知道我是否会报告它,因为无论如何都不推荐使用同步 XMLHttpRequests。

回答by scooterman

A common error is to send a https request on a server that's expecting a http connection.

一个常见的错误是在需要 http 连接的服务器上发送 https 请求。

回答by ShawnS

To get cross-domain AJAX calls to work, I have used PHP on the server ...

为了使跨域 AJAX 调用工作,我在服务器上使用了 PHP ...

getRemoteUrl.php

getRemoteUrl.php

<?php
if (preg_match("/^[();[]{}]+$/", $_GET['url'])) {
  //Something bad
} else {
  $ctx = stream_context_create(array('http' => array('timeout' => 10)));

  if (!(@$contents = file_get_contents($_GET['url'], 0, $ctx))) {
    //fail
  } else {
    //success
    echo $contents;
  }
}
?>

then call the PHP file and use the output as if you called the website directly.

然后调用 PHP 文件并使用输出,就像您直接调用网站一样。

function GetXmlHttpObject() {
  if (window.XMLHttpRequest) {
  // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  } else if (window.ActiveXObject) {
  // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  } else {
   return null;
  }
}

var xmlhttp = GetXmlHttpObject();
if (xmlhttp === null) {
  //Your browser does not support XMLHTTP!
} else {
  xmlhttp.open("GET", "getRemoteUrl.php" + "?url=someRemoteUrl", false);
  xmlhttp.send();
  var webpage = xmlhttp.responseText;
  //do something with webpage
}

I would recommend using asynchronous instead; I used synchronous for brevity. This can be done with other server-side languages if PHP is not preferred or available.

我建议改用异步;为简洁起见,我使用了同步。如果 PHP 不是首选或不可用的,则可以使用其他服务器端语言完成此操作。

回答by yonatan

to avoid the cross-domain policy of a server you can use Korz, just add

为了避免服务器的跨域策略,您可以使用Korz,只需添加

<script src="//tomodo-tools.s3.amazonaws.com/tomodo.korz-0.5.js"></script>

to your <head>and all cross domain requests will be routed througth tomodo.meso their Access-Control-Allow-Origin headeris '*'.

到您<head>和所有跨域请求将通过 tomodo.me路由,因此它们Access-Control-Allow-Origin header是“*”。

回答by AnK

DOM Exception 19 refers to a "Network Error". Is the URL that you are contacting in your Ajax call available?

DOM 异常 19 指的是“网络错误”。您在 Ajax 调用中联系的 URL 是否可用?

Cross domain requests are possible if the server supports it! Before making the actual GET/POST call, the browser issues an OPTIONS call to the server to check whether it supports cross domain requests. Check if the server that you are contacting supports cross domain requests. For more details on cross domain requests refer to this article which is very helpful: Using CORS

如果服务器支持,跨域请求是可能的!在进行实际的 GET/POST 调用之前,浏览器会向服务器发出 OPTIONS 调用以检查它是否支持跨域请求。检查您正在联系的服务器是否支持跨域请求。有关跨域请求的更多详细信息,请参阅这篇非常有用的文章:Using CORS