5***@qq.com
5***@qq.com
  • 发布:2016-07-12 11:31
  • 更新:2016-07-12 14:01
  • 阅读:2095

mui.ajax访问百度翻译接口出错

分类:MUI

http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4

我用jquery访问百度翻译接口时
var sign = MD5(str1);
var url = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
$.ajax({url:url,
data: {
"q": query,
"appid": appid,
"salt": salt,
"from": from,
"to": to,
"sign": sign
},
dataType: 'jsonp',
type: 'GET',
success: function (data) {
var r=data.trans_result[0];
mui.alert(r.src+"\n"+r.dst);
},
error:function(xhr,type,errorThrown){
//异常处理;
console.log(type)
}
});
可以返回成功

但是用mui.ajax 时
var sign = MD5(str1);
var url = 'http://api.fanyi.baidu.com/api/trans/vip/translate';
$.ajax(url,{
data: {
"q": query,
"appid": appid,
"salt": salt,
"from": from,
"to": to,
"sign": sign
},
dataType: 'jsonp',
type: 'GET',
success: function (data) {
var r=data.trans_result[0];
mui.alert(r.src+"\n"+r.dst);
},
error:function(xhr,type,errorThrown){
//异常处理;
console.log(type)
}
});

提示
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.

"abort"

2016-07-12 11:31 负责人:无 分享
已邀请:
BoredApe

BoredApe - 有问题就会有答案。

在mui.js后引入mui.jsonp.js
页面中使用getJSONP

  mui.getJSONP('http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4',{},function(e){  
            console.log(e);  
        }
  • 5***@qq.com (作者)

       我看了其他的帖子,没有改动源码,发现在真机上测试成功,但是直接在浏览器上调试是失败的,然后我产生了疑惑:mui.ajax()是不是需要基于plus基座比较容易成功?

    2016-07-12 16:43

pxl

pxl - 还不错

mui的ajax过来的数据乱码,解决不了

该问题目前已经被锁定, 无法添加新回复