2017-03-09 85 views
0

我使用python json http服务器并需要在此服务器上发布带有离子的json,但http post方法发送选项type.i需要发送post类型。什么是问题?http post在离子和python服务器

服务器:

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)); 
     }); 

蟒蛇服务器错误:

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

这看起来像一个跨域资源共享(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请求具有访问控制。

另一个好的SO文章可以在这里找到here