minibar: use AJAX instead of JSONP when possible

Using AJAX instead of JSONP can give better caching in the browser, since the
URL doesn't need to contain a (dynamically generated) callback function name.
But because of the same origin policy in JavaScript, AJAX can only be used when
the HTML file is served from the same server as the PGF. The new function
http_get_json in support.js uses AJAX if this is the case, and falls back to
JSONP otherwise.
This commit is contained in:
hallgren
2010-09-10 08:38:11 +00:00
parent f3a4b5b709
commit f5f8369d97
2 changed files with 65 additions and 20 deletions

View File

@@ -30,41 +30,37 @@ var server = {
switch_grammar: function(grammar_name) {
this.current_grammar_url=options.grammars_url+grammar_name;
},
get_grammarlist: function(cont) {
jsonpf(options.grammars_url+"grammars.cgi",cont);
http_get_json(options.grammars_url+"grammars.cgi",cont);
},
get_languages: function(cont) {
jsonpf(this.current_grammar_url,cont);
http_get_json(this.current_grammar_url,cont);
},
pgf_call: function(cmd,args,cont) {
var url=this.current_grammar_url+"?command="+cmd;
for(var arg in args) url+="&"+arg+"="+encodeURIComponent(args[arg]);
http_get_json(url,cont);
},
get_random: function(cont) {
jsonpf(this.current_grammar_url+"?command=random&random="+Math.random(),cont);
//jsonpf(this.current_grammar_url+"?command=random&random="+Math.random(),cont);
this.pgf_call("random",{random:Math.random()},cont);
},
linearize: function(tree,to,cont) {
jsonpf(this.current_grammar_url+"?command=linearize&tree="
+encodeURIComponent(tree)+"&to="+to,cont)
},
complete: function(from,input,cont) {
jsonpf(this.current_grammar_url
+"?command=complete"
+"&from="+encodeURIComponent(from)
+"&input="+encodeURIComponent(input),
cont);
this.pgf_call("complete",{from:from,input:input},cont);
},
parse: function(from,input,cont) {
this.pgf_call("parse",{from:from,input:input},cont);
},
translate: function(from,input,cont) {
jsonpf(this.current_grammar_url
+"?command=translate"
+"&from="+encodeURIComponent(from)
+"&input="+encodeURIComponent(input),
cont)
this.pgf_call("translate",{from:from,input:input},cont);
},
translategroup: function(from,input,cont) {
jsonpf(this.current_grammar_url
+"?command=translategroup"
+"&from="+encodeURIComponent(from)
+"&input="+encodeURIComponent(input),
cont)
this.pgf_call("translategroup",{from:from,input:input},cont);
}
};