我正在学习JQuery Get方法.我启动了一个Python HTTP服务器:
(只需键入命令" Python -m SimpleHTTPServer ").
只需在我的网络浏览器上访问"http:// localhost:80"即可测试此网络服务器.但是,当我写这个非常简单的JavaScript来访问我的网络服务器.我收到一条错误消息:
"代码501,消息不支持的方法('OPTIONS')"
我使用jquery.xdomainajax.js库,假设跨域请求JQuery.
这是我的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>
Run Code Online (Sandbox Code Playgroud)
实际上,如果我将您更改为任何其他网址,例如"http://www.google.ca".它运作得很好.但我不知道为什么它不适用于基本的Python HTTP服务器.谁能帮我?
我有这个 mootools 请求:
new Request({
url: 'http://localhost:8080/list',
method: 'get',
}).send();
Run Code Online (Sandbox Code Playgroud)
和一个小型 python 服务器,用以下方式处理它:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import subprocess
class HttpHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/list':
self.list()
else:
self._404()
def list(self):
self.response200()
res = "some string"
self.wfile.write(res)
def _404(self):
self.response404()
self.wfile.write("404\n")
def response200(self):
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Headers', 'X-Request, X-Requested-With')
self.send_header('Content-type', 'application/json')
self.end_headers()
def response404(self):
self.send_response(404)
self.send_header('Content-type', 'application/json')
self.end_headers()
def main():
try:
server = HTTPServer(('', 8080), HttpHandler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
当我尝试提出此请求时,我收到以下错误:
OPTIONS http://localhost:8080/ …Run Code Online (Sandbox Code Playgroud)