1
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.

4

3 回答 3

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);
        }
  });
});
于 2013-04-11T09:47:59.450 回答
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);
   }
});
于 2013-05-21T14:23:45.677 回答
-1

为了调用“MyCallback”函数,你的 url 应该是,

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

服务器应该返回 ajax 响应,例如

 return "MyCallback(your data here)"
于 2013-04-11T09:45:32.477 回答