javascript 奇怪的 JQuery 错误“代码 501,消息不支持的方法选项”

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

Strange JQuery Error "code 501, message Unsupported method OPTIONS"

javascriptjquerypythonhttphttpserver

提问by Xiao

I am learning the JQuery Get method. I start up a Python HTTP server:

我正在学习 JQuery Get 方法。我启动了一个 Python HTTP 服务器:

(just typing command "Python -m SimpleHTTPServer").

(只需输入命令“ Python -m SimpleHTTPServer”)。

It's fine to test this webserver by just visiting "http://localhost:80" on my web browser. However, when I write this very simple javascript to visit my webserver. I get an error message:

只需在我的网络浏览器上访问“http://localhost:80”即可测试此网络服务器。但是,当我编写这个非常简单的 javascript 来访问我的网络服务器时。我收到一条错误消息:

"code 501, message Unsupported method ('OPTIONS')"

“代码 501,消息不受支持的方法('选项')”

I use jquery.xdomainajax.js library which suppose cross domain request JQuery.

我使用 jquery.xdomainajax.js 库,它假设跨域请求 JQuery。

Here is my javascript code:

这是我的 javascript 代码:

<html>
<head>
<script src="jquery.min.js"></script>
<script src="jquery.xdomainajax.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  u = 'http://localhost:80';
 jQuery.get(u, function(res){       
    $("#data").html(res.responseText)
});
});


</script>
</head>
<body>
<p id="data"></p>
</body>
</html>

Actually, if I change u to any other url, such as "http://www.google.ca". It works quite well. But I have no idea why it does not work for the basic Python HTTP server. Can anyone help me?

实际上,如果我将您更改为任何其他网址,例如“http://www.google.ca”。它运作良好。但我不知道为什么它不适用于基本的 Python HTTP 服务器。谁能帮我?

回答by Xiao

What I do is to write a customized HTTPRequestHandler. I add a do-OPTIONSmethod inside MyHandler to tell browser my server support CORS. This is done by sending headers Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers. Also, I add a "self.send_header('Access-Control-Allow-Origin', '*')" statement in do_GETmethod.

我所做的是编写一个定制的HTTPRequestHandler。我在 MyHandler 中添加了一个do-OPTIONS方法来告诉浏览器我的服务器支持 CORS。这是通过发送标头Access-Control-Allow-Origin、Access-Control-Allow-Methods 和 Access-Control-Allow-Headers 来完成的。另外,我在do_GET方法中添加了“self.send_header('Access-Control-Allow-Origin', '*')”语句。

class MyHandler(BaseHTTPRequestHandler):
    def do_OPTIONS(self):           
        self.send_response(200, "ok")       
        self.send_header('Access-Control-Allow-Origin', '*')                
        self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
        self.send_header("Access-Control-Allow-Headers", "X-Requested-With")        

    def do_GET(self):           
        self.send_response(200)
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Content-type',    'text/html')                                    
        self.end_headers()              
        self.wfile.write("<html><body>Hello world!</body></html>")
        self.connection.shutdown(1) 

回答by Carlos

You may also need to add fields such as "Content-Type" to the allowed headers.

您可能还需要将诸如“Content-Type”之类的字段添加到允许的标题中。

self.send_header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type") 

回答by Sergio Cinos

Looks like a CORS preflight request (https://developer.mozilla.org/En/HTTP_access_control)

看起来像 CORS 预检请求 (https://developer.mozilla.org/En/HTTP_access_control)

I guess you are trying to access to a different domain/port. Depending on the request, the browser will send a preflight request (an OPTION request) to know if the server accepts the set of Headers or HTTP method you wanted to send in the first place. If the server responds OK, the browser will send the real request.

我猜您正在尝试访问不同的域/端口。根据请求,浏览器将发送预检请求(OPTION 请求)以了解服务器是否接受您首先要发送的标头集或 HTTP 方法。如果服务器响应 OK,则浏览器将发送真正的请求。

Looks like that Python server doesn't implement OPTIONs requests, hence the error.

看起来 Python 服务器没有实现 OPTIONs 请求,因此出现错误。

Tip: Network inspection tools (tcpdump, wireshark, ngrep...) help a lot when dealing with http requests and/or network errors.

提示:网络检查工具(tcpdump、wireshark、ngrep...)在处理 http 请求和/或网络错误时有很大帮助。

回答by freedev

It look like a Cross-Origin Resource Sharing (CORS) preflight request.

它看起来像一个跨域资源共享 (CORS) 预检请求。

Since CORS is a specification that is strongly related to a server configuration, I recommend to read http://enable-cors.org/

由于 CORS 是与服务器配置密切相关的规范,我建议阅读http://enable-cors.org/

There you'll see more about implementing CORS for your specific platform.

在那里你会看到更多关于为你的特定平台实施 CORS 的信息。