How can I use Google Translate via Ajax using jQuery?

前端 未结 6 1466
名媛妹妹
名媛妹妹 2021-01-24 21:43

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

相关标签:
6条回答
  • 2021-01-24 21:59

    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);
        }  
    });
    
    0 讨论(0)
  • 2021-01-24 22:10

    Just remove the key and see if it will work for you

    0 讨论(0)
  • 2021-01-24 22:12

    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.

    0 讨论(0)
  • 2021-01-24 22:16

    There is a plugin for jQuery that leverages the Google Translate API. It definately makes things easier and cleaner.

    jquery-translate

    0 讨论(0)
  • 2021-01-24 22:17

    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?

    0 讨论(0)
  • 2021-01-24 22:19

    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!

    0 讨论(0)
提交回复
热议问题