我自己的54001:
appid 和key出现错误, 先用的是百度提供的模板appid 及key 然后一直报错 之后用其它的appid 和key后能够正常运行
52003:这个需要自己申请一个appid,百度一下就可以了, 这里是百度翻译的开发文档:https://api.fanyi.baidu.com/
关于代码:
app.js:
var from = 'en';
var to = 'zh';
const the_md5 = require("./utiles/md5.js")
const appid = '20190528000302826'
const key = 'e2A2h832pG1PRubLj22_'
var salt = '1435660288';
var value = new Array();
App({
getwords: function (words,baidu) {
var query=words;
var str1 = appid + query + salt + key;
var sign = the_md5.MD5(str1);
// var sign ='384934B485C24C6FA01B6BFA95714A07';
console.log("app_words: ",words)
console.log("query= ", query)
console.log("str1= ", str1)
console.log("sign= ", sign)
const requestTask = wx.request({
url: 'http://api.fanyi.baidu.com/api/trans/vip/translate',
type: 'get',
data: {
q: words,
appid: appid,
salt: salt,
from:from,
to: to,
sign: sign
},
header: {
'content-type': 'application/json'
},
success: function (res) {
baidu(res.data)
console.log(res.data);
},fail(err){
console.log(err)
return;
}
})
}
})
开始没有加baidu这个参数,在page中一直无法从回调函数中取得数据,哎,
baidu只是一个自定义的符号(隐函数),page的js中用于接收参数
page的js中:
var app=getApp();
var value = new Array();
Page({
/**
* 页面的初始数据
*/
data: {
words_res:"",
word:""
},
input_tap1: function (e) {
console.log("显示结果:", e);
this.setData({
word: e.detail.value
})
}
,
viewtap1:function(){
var that = this;
app.getwords(this.data.word,function(data){
console.log("getwords_data: ",data)
that.setData({
words_res:'\n'+'结果为:'+data.trans_result[0].dst
})
})
}
})
var that=this,报错后才定义的,用this无法修改数据

8612

被折叠的 条评论
为什么被折叠?



