weixin_33737774 2013-08-02 07:27 采纳率: 0%
浏览 23

带有jsonp的jQuery Ajax

Hi all below is my code

$.ajax({
    'url': 'myUrl',
    'dataType': 'jsonp',
    crossDomain: true,
    'data': {
      // sample daa
    },
    'success': function (data) {
      alert("here::");
    },
    'error': function (jqxhr, status, errorThrown) {
      alert("Failure, Unable to recieve content : " + status)
      alert(jqxhr.responseText);
    })
})

I am using the jsonp as my url belongs to other domain and i am able to get the response code as 200 and i can able to see the data in response (Checcked in Firebug) but none of the success or error methods are executing.
Please help me on this

Update response(copied from firebug)

{"documents":[{"trans":"sdsd","orig":"How","translit":"Elā","src_translit":""}],"dict":[{"pos":"unknown","terms":["dgssdg","sgsd"],"entry":[{"word":"gsdg","reverse_translation":["method","treatment","recipe","attitude","how","retro"],"score":0.000305442198},{"word":"మార్గము","reverse_translation":["way","route","road","entry","how","impasse"],"score":0.000305442198}],"base_form":"how","pos_enum":20}],"src":"en","server_time":12}
  • 写回答

2条回答 默认 最新

  • weixin_33682719 2013-08-02 07:44
    关注

    This doesn't work because your server side script apparently is returning JSON instead of JSONP. Your server side script should be modified so that it returns JSONP if you want to be able to invoke it with a cross domain AJAX request.

    You can read more about JSONP here: http://en.wikipedia.org/wiki/JSONP

    So instead of:

    {
        "documents": [
            {
                "trans": "sdsd",
                "orig": "How",
                "translit": "Elā",
                "src_translit": ""
            }
        ],
        "dict": [
            {
                "pos": "unknown",
                "terms": [
                    "dgssdg",
                    "sgsd"
                ],
                "entry": [
                    {
                        "word": "gsdg",
                        "reverse_translation": [
                            "method",
                            "treatment",
                            "recipe",
                            "attitude",
                            "how",
                            "retro"
                        ],
                        "score": 0.000305442198
                    },
                    {
                        "word": "మార్గము",
                        "reverse_translation": [
                            "way",
                            "route",
                            "road",
                            "entry",
                            "how",
                            "impasse"
                        ],
                        "score": 0.000305442198
                    }
                ],
                "base_form": "how",
                "pos_enum": 20
            }
        ],
        "src": "en",
        "server_time": 12
    }
    

    your server side script should return:

    someCallback({
        "documents": [
            {
                "trans": "sdsd",
                "orig": "How",
                "translit": "Elā",
                "src_translit": ""
            }
        ],
        "dict": [
            {
                "pos": "unknown",
                "terms": [
                    "dgssdg",
                    "sgsd"
                ],
                "entry": [
                    {
                        "word": "gsdg",
                        "reverse_translation": [
                            "method",
                            "treatment",
                            "recipe",
                            "attitude",
                            "how",
                            "retro"
                        ],
                        "score": 0.000305442198
                    },
                    {
                        "word": "మార్గము",
                        "reverse_translation": [
                            "way",
                            "route",
                            "road",
                            "entry",
                            "how",
                            "impasse"
                        ],
                        "score": 0.000305442198
                    }
                ],
                "base_form": "how",
                "pos_enum": 20
            }
        ],
        "src": "en",
        "server_time": 12
    })
    

    where the client should be able to specify the name of the callback function (someCallback) by passing it as a query string parameter.

    Then you could perform the cross domain AJAX call:

    $.ajax({
        url: 'http://example.com/some_endpoint',
        jsonp: 'callback',
        dataType: 'jsonp',
        data: { firstName: 'john', lastName: 'smith' },
        success: function (result) {
            alert(result);
        },
        error: function (jqxhr, status, errorThrown) {
            alert('Failure, Unable to recieve content : ' + status)
            alert(jqxhr.responseText);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作