问题
I am using Ajax via jQuery, and I am trying to translate using the Google Translate Service. The service does not seem to work for me.
What am I doing wrong? How would I go about using this in my web application?
回答1:
You can do it like this:
$.ajax({
url: 'https://ajax.googleapis.com/ajax/services/language/translate',
dataType: 'jsonp',
data: { q: 'Hello world!', // text to translate
v: '1.0',
langpair: 'en|es' }, // '|es' for auto-detect
success: function(result) {
alert(result.responseData.translatedText);
},
error: function(XMLHttpRequest, errorMsg, errorThrown) {
alert(errorMsg);
}
});
回答2:
I think you are following the wrong strategy. You don't make AJAX calls to the Google Translate Service directly, the Google Translate Javascript API wraps this all for you.
Look at the examples in the API playground.
回答3:
Translate API from Google Translate v1.0 is not working anymore, v2.0 is what are you looking for and is not for free like google search or other services from them.
The price is 20$ for 1 million characters translated.
Here is the official info!
回答4:
What are some of the differences between your code and Google's example code?
Do the examples work for you?
Is it possible that you do not have a valid Google JavaScript API key?
回答5:
There is a plugin for jQuery that leverages the Google Translate API. It definately makes things easier and cleaner.
jquery-translate
回答6:
Just remove the key and see if it will work for you
来源:https://stackoverflow.com/questions/919522/how-can-i-use-google-translate-via-ajax-using-jquery