first we use jquery build a jsonp request like bellows
_pjs$.getJSON("http://localhost:8000/xhr_test2?x=1&callback=?", function(data) {
//alert("Symbol: " + data.symbol + ", Price: " + data.price);
console.log("DDDDDDDDDDDDDDDDD");
});
jquery will send the request as follows:
http://localhost:8000/xhr_test2?x=1&callback=jQuery16402700680344839699_1336379810409&_=1336379811771
yes,jquery will replace the callback after ? symbols with a auto generatered string
in our server-back we need capture the callback parameter and wrapit with callback(data) outside the return data
def xhr_test2(request):
callback=request.GET.get("callback")
print callback
logging.debug(callback)
if request.is_ajax():
message = "%s({'symbol': 'IBMajax', 'price': 91.42});"%callback
print message
logging.debug(message)
else:
message = "{'symbol': 'IBM', 'price': 91.42}"
message = "%s({'symbol': 'IBMajax', 'price': 91.42});"%callback
print message
logging.debug(message)
reference:
http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/
use jsonp we can solve cross-origin problem