简体   繁体   English

如何在requireJS中加载JSONP?

[英]How to load JSONP inside requireJS?

I'm trying to get a JSONP file to load in requireJS. 我正在尝试在requireJS中加载JSONP文件。 The docs say it should work like this: 文档说它应该像这样工作:

require(["http://example.com/api/data.json?callback=define"],
    function (data) {
        //The data object will be the API response for the
        //JSONP data call.
        console.log(data);
    }
);

My config.json looks like this (no line breaks): 我的config.json看起来像这样(没有换行符):

config({
    "default_language":"de-DE",
    "language_current":"de-DE",
    "language_selector":"translate",
    "language_set":"false"
});

So my require call should be: 所以我的要求应该是:

require(["http://example.com/api/config.json?config=define"],
    function (data) {
        //The data object will be the API response for the
        //JSONP data call.
        console.log(data);
    }
);

but doing so only returns config (the callback function!) is undefined . 但这样做只返回config (the callback function!) is undefined I have been sitting over this for a while now... and got it to work like this using the async plugin , but this syntax will not work, because I'm declaring a function outside of a define context. 我已经坐了一段时间了......并且使用async 插件让它像这样工作,但是这种语法不起作用,因为我在一个定义上下文之外声明了一个函数。

var config = function(data) {
    var settings = {};

  // create application settings
  settings.language_default = data.default_language || "en-EN";
  settings.language_current = data.language_current || priv.settings.language_default;
  settings.language_selector = data.selector || "translate";
  settings.language_set = data.language_set || false;

  console.log(settings);
  // return response object
  return settings;
};

define([
    'async!http://example.com/api/config.json!config'
  ],
  config
);

This seems to work, because now I specifically name the callback function config . 这似乎工作,因为现在我特别命名回调函数config

I guess everything would be ok, if I could pass in config as name of my define-callback function, but the only way I was able to do it is the above. 我想一切都会好的,如果我可以将config作为我的define-callback函数的名称define-callback ,但我能够做到的唯一方法是上面的。

Question : 问题
How do I correctly load a JSONP file in requireJS either using the default way by requireJS or the loader plugins provider here [ https://github.com/millermedeiros/requirejs-plugins/blob/master/examples/async.html] ? 如何使用requireJS的默认方式或者加载器插件提供程序在[ https://github.com/millermedeiros/requirejs-plugins/blob/master/examples/async.html]中正确加载requireJS中的JSONP文件?

This works perfectly fine with a static file loaded from S3: 对于从S3加载的静态文件,这非常适用:

define([],
    function () {
        $.ajax({
            url: "http://www.example.com/api/configuration.json",
            dataType: "jsonp",
            jsonpCallback: "config", /* Unique function name */
            success: function(data){
                /* Do something with data */
                console.log(data)
            }
        });
    }
);

So why does it not work when being done inside requireJS? 那么为什么在requireJS内部完成后它不起作用?

You are missing one thing: 你错过了一件事:

http://requirejs.org/docs/api.html#jsonp http://requirejs.org/docs/api.html#jsonp

You should use like this: 你应该这样使用:

require(["http://example.com/api/data.json?callback=define"],
    function (data) {
        //The data object will be the API response for the
        //JSONP data call.
        console.log(data);
    }
);

The callback is the object wrapped in the jsonp : callback是包装在jsonp的对象:

If your callback is define then use define 如果您的callback定义的,那么使用define

define({
    "default_language":"de-DE",
    "language_current":"de-DE",
    "language_selector":"translate",
    "language_set":"false"
});

Ok. 好。 This works. 这有效。

define(["async"],
    function () {
        callback = function(d) {
            console.log("callback called");
             console.log(d);
        };

        require([
            "async!http://www.example.com/api/configuration.json"
        ],
        callback
        );
    }
);

Maybe also someone else. 也许还有别人。

I had a similar problem , had to resort to using $.ajax() to load the gapi script. 我有一个类似的问题,不得不求助于使用$.ajax()来加载gapi脚本。 .

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

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