jQuery ajax请求错误:未调用回调

时间:2013-04-11 09:22:27

标签: javascript jquery asp.net ajax

window.MyCallback = function (data) {
   console.log(data);
};

$(document).ready(function () {

    $.ajax({
            type: 'GET',
            url: 'http://xxx.xxx.xxx.xx/MyService/Service.svc/MyMethod',
            contentType: 'application/jsonp',
            crossdomain: true,
            dataType: "jsonp",
            data: { 'username': 'Jack' },
            crossDomain: true,
            jsonpCallback: 'MyCallback',
            success: function (txt) {
                console.log(txt);
            },
            error: function (xhr, status, err) {
                console.log(status, err);
            }
    });
});

我总是收到错误:MyCallback was not called.

3 个答案:

答案 0 :(得分:1)

为什么不喜欢这个?

var mycallback = function (data) {
  console.log("callback", data);
};

$(document).ready(function () {

  $.ajax({
        type: 'GET',
        url: 'https://graph.facebook.com/sagish',
        contentType: 'application/jsonp',
        dataType: "jsonp",
        data: { 'username': 'Jack' },
        crossDomain: true,
        success: mycallback,
        error: function (xhr, status, err) {
            console.log(status, err);
        }
  });
});

答案 1 :(得分:0)

$.ajax({
   type: "GET",
   dataType: "jsonp",
   contentType: "application/javascript",
   data: { 'username': 'Jack' },
   async: false,
   url: 'http://xxx.xxx.xxx.xx/MyService/Service.svc/MyMethod',
   success: function (jsonData) {
       console.log(jsonData);
   },
   error: function (request, textStatus, errorThrown) {
       console.log(request.responseText);
       console.log(textStatus);
       console.log(errorThrown);
   }
});

答案 2 :(得分:-1)

要调用“MyCallback”功能,您的网址应为

url: 'http://xxx.xxx.xxx.xx/MyService/Service.svc/MyMethod?callback=?

服务器应该返回像

这样的ajax响应
 return "MyCallback(your data here)"