简体   繁体   English

完成后,jQuery AJAX jsonp返回响应

[英]jQuery AJAX jsonp return response after done

Its similar question from here How do I return the response from an asynchronous call? 这里类似的问题, 如何从异步调用返回响应?

in Ajax JSONP, how do i get the response after done? 在Ajax JSONP中,完成后如何获得响应?

https://jsfiddle.net/zerolfc/svwxm5tt/ 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

The JSONP "protocol" relies on the site replying to your request with a JavaScript statement of the form, JSONP “协议”依赖于网站使用以下形式的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. 该函数的名称作为代码中的参数提供,其想法是,响应脚本一旦被浏览器使用并解释,将导致使用已解析的JSON Blob调用该函数-也就是说,一个JavaScript对象。 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). jQuery库将为您完成一些bookeeper工作,甚至可以创建要调用的全局作用域函数(该代码只是将您提供的回调作为“成功”参数调用的代码)。

Example

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

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