浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

posted on 2012-05-07 16:44  lexus  阅读(188)  评论(0编辑  收藏  举报