I'm trying to get a JSONP file to load in requireJS. 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({
"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
. 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.
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
.
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.
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]?
This works perfectly fine with a static file loaded from 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?