Reputation: 2733
I have a bit of a challenging problem. I'm using the Blogger JSON API to get JSON data and display on the page. Normally it's very straightforward:
<script src="http://blog-name.blogspot.com/feeds/posts/default?alt=json-in-script&callback=renderPost&max-results=4"></script>
You embed this script on a page which requests the data and passes a data object to the javascript callback (in this case the callback is called renderPost
).
The problem is that I am using the Closure compiler to compile the rest of my JS. So, renderPost
is obfuscated.
How would you go about mimicking this behavior with javascript? Can you use an AJAX request to get the JSON object?
Part of the problem is I don't really know what Blogger is doing here, so an explanation of that would be helpful too.
Upvotes: 0
Views: 208
Reputation: 9361
It's doing jsonp, and this is currently the only way to get json from other domain. No, you cannot use regular ajax request for cross-domain communication. See Rob W answer on how to solve your problem.
Upvotes: 1
Reputation: 349042
In the Closure compiler, you can export the variable as follows:
window['varname'] = varname;
For example:
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==
window['renderPost'] = renderPost;
function renderPost(name) {
return prompt('', '');
}
compiles to:
window.renderPost=a;function a(){return prompt("","")}a();
instead of (when omitting window['renderPost'] = renderPost
):
prompt("","");
Upvotes: 3