2017-03-09 97 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