我的JSON-RPC客户端(使用Dojo JSON-RPC浏览器)在我的JSON-RPC请求(Dojo.CallRemote)上向我的JSON-RPC服务器发出 myserver.com/12345 (Python 2.5,SimpleJsonRPCServer)。

然后,该服务器使用标题为“ Options / HTTP / 1.1”的HTTP请求,默认情况下无法处理,因此我为此请求编写了一个自定义处理程序。

浏览器的请求标头说:

OPTIONS / HTTP/1.1
Host: myserver:12345
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.8) Gecko/20100214 Linux Mint/8 (Helena) Firefox/3.5.8 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.7,de;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Origin: http://myserver.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with

和我发送的回应看起来像:

HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.5
Date: Mon, 05 Apr 2010 18:58:34 GMT
Access-Control-Allow-Method: POST
Access-Control-Allow-Headers: POST
Allow: POST
Content-Type: application/json-rpc
Content-length: 0

但是在浏览器中,我会收到以下错误:

错误:无法加载 http://myserver.com:12345 状态:0

我验证了JSON服务是可以从网络上到达的。

现在的问题是,浏览器(例如,Firefox)期望听起来有什么回应?还是问题在其他地方?

有帮助吗?

解决方案

看到 CORS规范.

(顺便说一句;有HTTP的标题注册表,请参阅 http://www.iana.org/assignments/message-headers/prov-headers.htmlhttp://www.iana.org/assignments/message-headers/perm-headers.html, ,这本来可以将您指向正确的规格)。

其他提示

添加代码并尝试,对我来说很好:

class CGIHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
...
...
    def do_OPTIONS(self):
        self.send_response(200, "ok")
        self.send_header('Access-Control-Allow-Origin', self.headers.dict['origin'])
        self.send_header('Access-Control-Allow-Methods', 'POST, OPTIONS')

检查我的代码。它适用于在Chrome浏览器中运行的客户端JavaScript代码。

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) 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top