How can I use Google Translate via Ajax using jQuery?

前端 未结 6 1525
名媛妹妹
名媛妹妹 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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);
        }  
    });
    

提交回复
热议问题