2017-03-09 9 views
0

私はpython json httpサーバーを使用し、このサーバーでionicとポストjsonが必要ですが、http postメソッドは、オプションtype.i送信post type.whats問題を送信する必要がありますか?イオンとpythonサーバーでhttpポスト

サーバー:

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メソッド:Pythonのサーバーから

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

エラー:

これはクロスオリジンリソース共有(CORS)プリフライトのように見えます
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

リクエストの問題。私はあなたのクラスに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です。

関連する問題