【问题标题】:How to respond to HTTP OPTIONS request on a JSON-RPC server如何响应 JSON-RPC 服务器上的 HTTP OPTIONS 请求
【发布时间】:2010-04-05 19:05:22
【问题描述】:

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

然后服务器会收到一个标头为“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 status:0

我已验证 JSON 服务可从网络访问。

现在的问题是,浏览器(比如 Firefox)期望听众的回应是什么?或者问题可能出在其他地方?

【问题讨论】:

    标签: python http dojo json-rpc


    【解决方案1】:

    请参阅CORS Specification

    (顺便说一句;有一个 HTTP 标头注册表,请参阅 http://www.iana.org/assignments/message-headers/prov-headers.htmlhttp://www.iana.org/assignments/message-headers/perm-headers.html,这会为您指明正确的规范)。

    【讨论】:

      【解决方案2】:

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

      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')
      

      【讨论】:

        【解决方案3】:

        检查我的代码。它适用于在 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) 
        

        【讨论】:

          猜你喜欢
          • 2012-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多