Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to load JSON? #101

Closed
migliori opened this issue Nov 18, 2019 · 2 comments
Closed

Is it possible to load JSON? #101

migliori opened this issue Nov 18, 2019 · 2 comments

Comments

@migliori
Copy link

Hi,

I can't figure out how to load a JSON file and access the JSON object, is it possible?
Perhaps it would be interesting to be able to code something like this?:

loadjs(['json!/path/to/file.json'], function(obj) {
    /* file.json loaded as json */
    console.log(obj);
});
@amorey
Copy link
Member

amorey commented Nov 18, 2019

LoadJS uses the browser's built-in tags (<script>, <style>, <img>) to load files and as far I know you can't load JSON directly using those tags. However, you can use the jsonp method in conjunction with LoadJS if that's useful:

Client code:

window.callbackFn = function(json) {
  console.log(json);
}

loadjs('/server-endpoint', function() {
  // server has responded and callbackFn has been called
});

Server reponse:

callbackFn({
  mykey1: myval1,
  mykey2: myval2,
  ...
});

@migliori
Copy link
Author

Thanks for your answer, in most cases it would be a good solution
(unfortunately not for me, I'm inside a tinyMce plugin dialog iFrame with TypeScript, so I'll use XMLHttpRequest instead)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants