0

i use python json http server and need post json with ionic in this server,but http post method send options type.i need send post type.whats the problem?

Server:

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

ionic http post method:

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

Error from python server:

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 -
DanialDP
  • 25
  • 1
  • 11

1 Answers1

0

This looks like a Cross-Origin Resource Sharing (CORS) preflight request issue.

I would recommend adding a do_OPTIONS method to your class:

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

This will tell your browser that the POST request has Access-Control.

Another good SO article on this can be found here.

Community
  • 1
  • 1
Sam Gomena
  • 1,450
  • 11
  • 21