1

I want to change this code in python to $.post of jQuery

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import httplib, urllib, json

words = u'Hello'
request = {
    'api_key': 'YOUR API KEY',
    'params': [words]
}
params = json.dumps(request)

headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "application/json"}

conn = httplib.HTTPConnection("abc.com:3000")
conn.request("POST", "/service/rest/", params, headers)

response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()
print data

Like This

$.post("abc.com:3000/service/rest/", );

Thank for Help. I have never learned Python.

1 Answers1

0
var words = ['Hello']

$.post("abc.com:3000/service/rest/",
    JSON.stringify({
        'api_key': 'YOUR API KEY',
        'params' : words
    }), function(response){
        console.log(response)
    }
)

console.log(response) should display the response from the server in the JS console.

Note: This will not work across domains or subdomains due to the Same Origin Policy for Javascript. If this applies to you, you might want to take a look at JSONP

Community
  • 1
  • 1
Huey
  • 5,110
  • 6
  • 32
  • 44