我使用python json http服务器并且需要在此服务器中使用离子json,但http post方法发送选项type.i需要发送post type.whats问题?
服务器:
def do_POST(self):
content_len = int(self.headers.getheader('content-length'))
post_body = self.rfile.read(content_len)
self.send_response(200)
self.end_headers()
data = json.loads(post_body)
self.wfile.write(data['id'])
return
离子http post方法:
$http.post(url, JSON.stringify({"id":"1"})).success(function (res) {
console.log("res" + res);
}).error(function (data, status, headers, config) {
console.log(data, status, headers,JSON.stringify (config));
});
来自python服务器的错误:
192.168.1.4 - - [10/Mar/2017 02:36:28] code 501, message Unsupported method ('OPTIONS')
192.168.1.4 - - [10/Mar/2017 02:36:28] "OPTIONS / HTTP/1.1" 501 -
答案 0 :(得分:0)
这看起来像是跨源资源共享(CORS)预检请求问题。
我建议在你的班级中添加一个do_OPTIONS方法:
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")
这将告诉您的浏览器POST请求具有访问控制权。
关于此问题的另一篇好文章可以找到here。