javascript jQuery JSONP 不调用回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11175029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:16:43  来源:igfitidea点击:

jQuery JSONP not calling the callback

javascriptjqueryjsonp

提问by user1437328

I am having some problem with jsonp and jquery.

我在使用 jsonp 和 jquery 时遇到了一些问题。

This is my code -

这是我的代码 -

var myCallback = function(data) {
  console.log(data);
};

$.ajax({
  url: my_url,
  type: 'GET',
  dataType: 'jsonp',
  jsonp: 'callback',
  jsonpCallback: 'myCallback'
});

jQuery adds something like ?callback=myCallback&_=1340513330866to my_url and the data returned from my_url is myCallback('abcd')- although in reality it will be returning some HTML code instead of the abcd.

jQuery?callback=myCallback&_=1340513330866向 my_url添加了类似的东西,从 my_url 返回的数据是myCallback('abcd')- 尽管实际上它会返回一些 HTML 代码而不是abcd.

Problem: abcdis not logged in console through myCallback. So what am i doing wrong ? I was under the impression that the data returned will be executed as it is inside script tags ?

问题:abcd未通过myCallback. 那么我做错了什么?我的印象是返回的数据将在脚本标签内执行?

回答by Felix Kling

If you use your own function, then you have to explicitly declare it as global. For example:

如果您使用自己的函数,则必须将其显式声明为global。例如:

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

DEMO

演示



Explanation

解释

Every function that should be called in response to a successful JSONP request must be global. jQuery is doing this as well. This is because JSONP is nothing else than including a (dynamically generated (most of the time)) JavaScript file with a <script>tag, which only contains a function call. Since each script is evaluated in global scope, the function that is called must be global as well.

响应成功的 JSONP 请求而应调用的每个函数都必须是全局的。jQuery 也在这样做。这是因为 JSONP 只不过是包含一个(动态生成的(大部分时间))带有<script>标记的JavaScript 文件,该文件仅包含一个函数调用。由于每个脚本都在全局范围内评估,因此调用的函数也必须是全局的。

回答by marcdahan

1) move the single quote from the called method (as Umesh Aawte wrote)

1)从被调用的方法中移动单引号(如 Umesh Aawte 所写)

2) make the callback global

2) 使回调全局化

3) your callback is a part of jQuery now so thisis your way to get your datas

3) 你的回调现在是jQuery 的一部分,所以是你获取数据的方式

hereafter the solution: (using jQuery : v3.3.1, node : v6.10.0, express : v4.16.3

此后的解决方案:(使用jQuery:v3.3.1,节点:v6.10.0,表达:v4.16.3

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

$.ajax({
  url: my_url,
  type: 'GET',
  dataType: 'jsonp',
  jsonp: 'myCallback',
  jsonpCallback: myCallback
});

that's all folks!

这就是所有的人!

回答by Umesh Aawte

Remove single quote from the called method this will work, Please check the code here,

从被调用的方法中删除单引号这将起作用,请在此处检查代码,

var myCallback = function(data) {
      console.log(data);
    };

$.ajax({
  url: my_url,
  type: 'GET',
  dataType: 'jsonp',
  jsonp: 'callback',
  jsonpCallback: myCallback
});

Try this fiddle

试试这个小提琴

回答by matt3141

Why not simply:

为什么不简单:

$.getJSON(my_url, myCallback);

this will handle function scoping and looks much simpler

这将处理函数作用域并且看起来更简单