jQuery:我收到 OPTIONS 请求而不是 GET

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

jQuery: I get OPTIONS request instead of GET

jquery

提问by Kurund Jalmi

I am using simple jQuery

我正在使用简单的 jQuery

$.get( .... );

Here instead of getting GET response I get OPTIONS.( checked in firebug Net)

在这里,我得到的不是 GET 响应,而是 OPTIONS。(在 firebug Net 中检查)

Same code is working fine in Safari. Looks like some problem with Firefox.

相同的代码在 Safari 中运行良好。Firefox 看起来有些问题。

Any workaround / solutions to fix this problem..

解决此问题的任何解决方法/解决方案..

Thanks

谢谢

Kurund

库伦德

回答by KARASZI István

The OPTIONSrequest what you see is the preflight request, you can read about that here:

OPTIONS你看到的请求是预检请求,你可以在这里阅读:

It's there because you're requesting a cross-domain XMLHttpRequest so the browser has to check whether your request is allowed on the remote server or not.

它在那里是因为您正在请求跨域 XMLHttpRequest,因此浏览器必须检查远程服务器上是否允许您的请求。

There are two solutions to solve the problem (as mentioned above):

有两种解决方案可以解决问题(如上所述):

  • implement the response for the OPTIONSrequest with the corresponding Access-Control-*headers
  • use a JSONPrequest instead of simple JSON
  • OPTIONS使用相应的Access-Control-*标头实现对请求的响应
  • 使用JSONP请求而不是简单的 JSON

回答by fitzgeraldsteele

This is likely due to restrictions on Javascript doing cross-domain XMLHttpRequests. This is generally not allowed for security reasons. See the question referenced above, or a similar question I asked.

这可能是由于 Javascript 执行跨域 XMLHttpRequests 的限制。出于安全原因,这通常是不允许的。请参阅上面引用的问题,或我问类似问题

To solve this problem:

要解决这个问题:

Hope that helps!

希望有帮助!

回答by Moak

I had the same issue, the cause I figured was in the html <head>section I had set the base element to this

我遇到了同样的问题,我认为原因是在 html<head>部分中,我已将基本元素设置为此

<base href="http://local.develepment.url" />

Which I changed to

我改成

<base href="http://<?php echo $_SERVER['HTTP_HOST']?>/" />

回答by Bala

You are sending request to cross domain.

您正在向跨域发送请求。

For cross-domainrequests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plainwill trigger the browser to send a preflight OPTIONSrequest to the server.

对于跨域请求,将内容类型设置为application/x-www-form-urlencodedmultipart/form-datatext/plain 以外的任何内容将触发浏览器向 服务器发送预检 OPTIONS请求。

So you may need change specify contentTypeto avoid OPTION request. Example:-

因此,您可能需要更改指定contentType以避免 OPTION 请求。例子:-

$.ajax({
    url: "crossdomainurl",
    type: "GET",
    contentType: 'text/plain'
});