我是python的新手,我试图通过jquery ajax调用使用http请求从python脚本中获取数据。但是,我收到以下错误,因为python脚本不允许跨域请求。
阻止跨源请求:同源策略禁止在http://somehost.com:8000/gen.py读取远程资源。 (原因:CORS标题' Access-Control-Allow-Origin'缺失)。
我不确定我需要在下面的脚本中更改服务器跨域请求。为了清楚起见,我使用的是安装在Red Hat Enterprise Linux Server 6.5(Santiago)上的python v2.6.6。下面是我到目前为止尝试过的python服务器代码。
#!/usr/bin/env python
import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable() ## This line enables CGI error reporting
server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = ["/"]
httpd = server(server_address, handler)
httpd.serve_forever()
以下是来自html文件的jquery代码,该文件位于不同的服务器上:
$(document).ready(function(){
$("button").click(function(){
$.ajax({
url: "http://somehost.com:8000/gen.py",
type: 'GET',
success: function(data) {
console.log(data);
},
error: function(e) {
console.log(e.message);
}
});
});
});
任何指向此的指针都是值得注意的。如果我应该提供更多细节,请告诉我。
提前谢谢。