I'm currently developping a GreaseMonkey user script to provide a direct translation of some form fields inside an Intranet App.
Everything goes OK until I call Google Translation API using this 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 are my problems :
- I'm forced to use JSONP as this is a Cross-Domain request. In order to do that, I added the
&callback=?
string at the end of my URL. ThegetJSON
callback isn't fired (but the response data is correct), and I get this error in the Firebug console :
jsonp1298988446807 is not defined
If I use a
&callback=foo
instead, FF doesn't seem to like it, as my request is no longer a POST one, it doesn't complete but it shows (in Network panel)OPTIONS request_url 405 Method Not Allowed
If I create a custom function to specify as callback, it doesn't work either as the function isn't called (it only contains an alert to check if it works).
If anyone has the slightest idea why this doesn't work, please help me, because i'm this close to banging my head on the wall (maybe it would help ^^).
Thanks.
EDIT : Thanks to Scoobler, I believed I've gone a little bit further. Using his code, i've managed to get something more than a
/ignore
from my script ^^
The request does not appear in the network tab of Firebug, and the responses given by the alerts are :
Response text: undefined
Status returned: error
Error thrown: Error thrown: [Exception... "Component is 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 the mean time, I kept researching on my own, and came across a jQuery/GreaseMonkey bridge for cross-domain requests, with a complete walkthrough here (from this post), but this shows the exact same error than Scoobler's script