//Online translation query (Baidu translation) $('#selectBtn').click(function() { var appid = 'The appid you applied for'; var key = 'The key you applied for'; var salt = (new Date).getTime();//Take the current time as a random number var query= $('#transInput').val();//Get the val of the input box var q = encodeURIComponent(query);//Encode UTF-8 var from = $('#languageLeft').attr('data-language');//原文 var to = $('#languageRight').attr('data-language');//Translation var str1 = appid + query + salt +key;//key var sign = MD5(str1);//md5 encryption $.ajax({ url: 'http://api.fanyi.baidu.com/api/trans/vip/translate', type: 'get', dataType: 'jsonp', data: { q: query, appid: appid, salt: salt, from: 'auto', to: 'auto', sign: sign }, success: function (data) { console.log(data); var result = data.trans_result; $('#transDisplay').empty(); for(var i in result){ var html = '<p>'+decodeURIComponent(result[i].dst)+'</p>'; $('#transDisplay').append(html); } } }); });
The official website gave the appid and key for the test
var appid = '2015063000000001';
var key = '12345678';
Also download an md5 file, you can go to the official website to click the demo download, which contains the MD5 file.
The from and to above are set to auto, which automatically recognizes the language. If you want to pass it yourself, you can refer to the variables from and to that I set above. I set the data-language attribute on the html tag to store the language encoding.
There is a problem to note here, the text you want to translate needs to be encoded and transcoded during ajax transmission, but this piece does not need to be transcoded when generating the secret key. Everyone remember.