jsonp - jQuery, JSON-P and Google Translation : Various errors -
i'm developping greasemonkey user script provide direct translation of form fields inside intranet app.
everything goes ok until call google translation api using code :
var apiurl = 'https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=fr%7cen&q='; $.getjson(apiurl+encodeuricomponent(text)+"&callback=?",function(data){ alert('translation complete'); //change text });
here problems :
- i'm forced use jsonp cross-domain request. in order that, added
&callback=?
string @ end of url.getjson
callback isn't fired (but response data correct), , error in firebug console :
jsonp1298988446807 not defined
if use
&callback=foo
instead, ff doesn't seem it, request no longer post one, doesn't complete shows (in network panel)options request_url 405 method not allowed
if create custom function specify callback, doesn't work either function isn't called (it contains alert check if works).
if has slightest idea why doesn't work, please me, because i'm close banging head on wall (maybe ^^).
thanks.
edit : scoobler, believed i've gone little bit further. using code, i've managed more
/ignore
script ^^ the request not appear in network tab of firebug, , responses given alerts :
response text: undefined
status returned: error
error thrown: error thrown: [exception... "component not available" nsresult: "0x80040111 (ns_error_not_available)" location: "js frame :: file:///c:/documents%20and%20settings/username/application%20data/mozilla/firefox/profiles/jmbr7ut9.default/extensions/%7be4a8a97b-f2ed-450b-b12d-ee082ba24781%7d/components/greasemonkey.js :: anonymous :: line 396" data: no]
in mean time, kept researching on own, , came across jquery/greasemonkey bridge cross-domain requests, complete walkthrough here (from this post), shows exact same error scoobler's script
maybe try - it's more verbose syntax .ajax()
don't have encode parameters yourself:
var apiurl = 'https://ajax.googleapis.com/ajax/services/language/translate'; var text = 'il fonctionne parfaitement'; $.ajax({ url: apiurl, datatype: 'jsonp', data: { v: "1.0", langpair: "fr|en", q: text }, success: function(data) { var translated = data.responsedata.translatedtext; alert('translation complete: ' + translated); } });
see demo.
Comments
Post a Comment