我是新來的python,我試圖通過jquery ajax調用使用http請求從python腳本獲取數據。但是,由於python腳本不允許跨域請求,我得到了下面的錯誤。Python - 訪問控制允許起源
跨源請求被阻止:同源策略不允許讀取遠程資源http://somehost.com:8000/gen.py。 (原因:缺少CORS頭'Access-Control-Allow-Origin')。
我不確定我需要在下面的腳本中更改服務器跨域請求。清楚的是,我使用的是安裝在紅帽企業Linux服務器版本6.5(聖地亞哥)上的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);
}
});
});
});
任何指針,這是明顯的。讓我知道我是否應該提供更多細節。
預先感謝您。
什麼是您的基礎設施? javascript如何提供服務?你的web應用程序沒有使用python嗎? – e4c5
@ e4c5 python腳本位於linux上 –
這不是我所要求的。我在問你的javascript是否在一個web服務器上,而你的python腳本是在另一個 – e4c5