2012-04-25 19 views
3

我正在访问一个跨页面的web服务,利用来自html页面的an.ajax jquery调用。虽然我可以使用萤火虫查看jsonp数据,但我无法将其加载到变量中,甚至无法显示它(出于调试目的)。试图使用jsonpCallback检索数据,成功和完整的函数总是会导致'undefined'/ null数据。ajax'GET'调用返回jsonp没关系,但是回调产生'undefined'数据

最终,我需要将数据保存到变量。任何援助将不胜感激!

$.ajax({ 
    data: { 
     User: UserValue, 
     GUID: GUIDValue 
    }, 
    cache: false, 
    dataType: "jsonp", // tried json 
    type: "GET", 
    crossDomain: true, 
    jsonp: false, // tried true 
    jsonpCallback: function (saveData) { 
     if (saveData == null) { 
      alert("DATA IS UNDEFINED!"); // displays every time 
     } 
     alert("Success is " + saveData); // 'Success is undefined' 
    }, 
    url: "http://localhost/NotifMOD/NotifService.svc/GetAllMessages?callback=success?", 
    async: false, // tried true 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     console.log(textStatus, errorThrown); 
    }, 
    complete: function (a, b) { 
     alert(a); //[object Object] 
     alert(b); // parseerror 
    } 
}); 

回答

3

在JSONP中,你必须在你的代码中定义你的函数。

jsonpCallback必须是该函数的名称,而不是函数。

http://api.jquery.com/jQuery.ajax/

你不喜欢这样:

function receive(saveData) { 
    if (saveData == null) { 
      alert("DATA IS UNDEFINED!"); // displays every time 
    } 
    alert("Success is " + saveData); // 'Success is undefined' 
} 

$.ajax({ 
    data: { 
     User: UserValue, 
     GUID: GUIDValue 
    }, 
    cache: false, 
    dataType: "jsonp", // tried json 
    type: "GET", 
    crossDomain: true, 
    jsonp: false, // tried true 
    jsonpCallback: "receive", 
    url: "http://localhost/NotifMOD/NotifService.svc/GetAllMessages?callback=receive?", 
    async: false, // tried true 
    error: function (XMLHttpRequest, textStatus, errorThrown) { 
     console.log(textStatus, errorThrown); 
    } 
}); 
+0

感谢您的答复 “dystroy”。我尝试了您的建议(添加了将URL更改为_http://localhost/NotifMOD/NotifiService.svc/GetAllMessages?callback = jsonpCallback?)。现在错误函数'errorThrown'参数返回错误:接收未被调用 – rsamoose 2012-04-25 20:50:38

+0

函数的名称(这里是“receive”)必须是jsonp答案开头的那个。 (假设服务器使用“callback”参数),url应该以“callback = receive”结尾 – 2012-04-26 06:01:14

+0

也许它会帮助你:我制作的一个开源代码客户端:https://github.com/Canop/ braldop/blob/master/chrome/braldop/inext_com.js和服务器:https://github.com/Canop/braldop/blob/master/go/src/braldopserver/BraldopServer.go – 2012-04-26 06:03:30