gpt4 book ai didi

javascript - jQuery AJAX jsonp 完成后返回响应

转载 作者:行者123 更新时间:2023-12-03 04:19:50 25 4
gpt4 key购买 nike

这里有类似的问题How do I return the response from an asynchronous call?

在 Ajax JSONP 中,完成后如何获取响应?

https://jsfiddle.net/zerolfc/svwxm5tt/

class Api {

constructor() {

}

yahoo(query) {

}

jsfiddle(query){

let result = '';

$.ajax({
url: 'https://jsfiddle.net/echo/jsonp/',
dataType: 'jsonp',
jsonpCallback: 'jsonp',
data: {
query: 'query',
format: 'json'
},
}).done(function(response) {
result = response;
console.log(result);
});

return result;

}

}

$api = new Api;

console.log( $api.jsfiddle() ); // empty

最佳答案

JSONP“协议(protocol)”依赖于网站使用以下形式的 JavaScript 语句回复您的请求,

 someFunction( someJSON )

The name of the function is supplied as an argument from your code, with the idea being that the response script, once consumed and interpreted by the browser, will result in a call to that function with a parsed blob of JSON — which is to say, a JavaScript object. The jQuery library will do some of the bookeeping work for you, even to the extent of creating the globally-scoped function to call (which will be code that just calls the callback you supply as the "success" argument).

Example

关于javascript - jQuery AJAX jsonp 完成后返回响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43997613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com