简体   繁体   English

JSONP ajax回调失败

[英]JSONP ajax callback failing

I have following script running on my local test drive. 我在本地测试驱动器上运行了以下脚本。 It imports a JSON file and builds a webpage, but for some reason, there is an error in handling the data. 它导入JSON文件并构建网页,但由于某种原因,处理数据时出错。 No data is shown... 没有数据显示......

But whenever I use (the same) data hard-coded, I get the result I want. 但每当我使用(相同的)数据硬编码时,我得到我想要的结果。 So this made me think it has something to do with the way I handle the JSON import... 所以这让我觉得它与我处理JSON导入的方式有关...

This is my AJAX callback: 这是我的AJAX回调:

getGames: function(fn) {
     $.ajax({
          type: "GET",
          url: App.Config('webserviceurl')+'games-payed.js',
          dataType: "jsonp",
          success: function (data) {
              console.log('streets', data);
              if(fn){
                  fn(data);
              }
          },
          error: function (msg) {
              if(fn){
                  fn({status:400});
              }
          }
     });
}

And this code isn't working, nor am I getting any errors in my console... 这段代码不起作用,我的控制台也没有出现任何错误......

When I load the data hard coded, it works perfectly: 当我加载硬编码的数据时,它完美地工作:

getGames: function(fn) {
     var games = App.Config('dummyGames');

     if(fn){
          fn(games);
     }
}  

Is there something wrong with my AJAX callback? 我的AJAX回调有问题吗?

EDIT: The JSON file looks like this: 编辑: JSON文件如下所示:

jsonp1({
    "status": "200",
    "data": [
        {
            "id": "1",
            "title": "Title 1",
            "publishDate": "2013-03-27T15:25:53.430Z",
            "thumbnail": "images/thumbs/image_game1.png",
            "html5": "http://mysite.com/index.html"
        },
        {
            "id": "2",
            "title": "Title 2",
            "publishDate": "2013-03-20T15:25:53.430Z",
            "thumbnail": "images/thumbs/image_game2.png",
            "html5": "http://mysite.com/index.html"
        },
        {
            "id": "3",
            "title": "Title 3",
            "publishDate": "2013-03-18T15:25:53.430Z",
            "thumbnail": "images/thumbs/image_game3.png",
            "html5": "http://mysite.com/index.html"
        }

    ]
});

In your example, I see that you wrap your json data inside jsonp1 . 在您的示例中,我看到您将json数据包装在jsonp1 I suppose that is a fixed name. 我想这是一个固定的名字。 If that's the case, try this: 如果是这种情况,试试这个:

getGames: function(fn) {
     $.ajax({
          type: "GET",
          url: App.Config('webserviceurl')+'games-payed.js',
          jsonp: false,
          jsonpCallback:"jsonp1",
          dataType: "jsonp",
          success: function (data) {
              console.log('streets', data);
              if(fn){
                  fn(data);
              }
          },
          error: function (msg) {
              if(fn){
                  fn({status:400});
              }
          }
     });
}

Notice the jsonpCallback:"jsonp1" and jsonp: false . 注意jsonpCallback:"jsonp1"jsonp: false The reason for this is: by default, jquery will generate the callback function name automatically and randomly and append ?callback=generatedFunctionName to the end of your url. 原因是:默认情况下,jquery将自动并随机生成回调函数名称,并将?callback=generatedFunctionName附加到url的末尾。 Thanks to the callback parameter, the code on server side could use the same function name to call the callback on browser. 由于回调参数,服务器端的代码可以使用相同的函数名称来调用浏览器上的回调。

In your case, you're using fixed function name (jsonp1), so you have to: 在您的情况下,您使用的是固定函数名称(jsonp1),因此您必须:

  • Specify your function name explicitly using jsonpCallback="jsonp1" 使用jsonpCallback="jsonp1"显式指定函数名称
  • Set jsonp = false to prevent jQuery from adding the "?callback" string to the URL or attempting to use "=?" 设置jsonp = false以防止jQuery将“?callback”字符串添加到URL或尝试使用“=?” for transformation. 转型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM