以下小提琴没有给出预期的json回复:
相反,我得到了:
{“items”:[{“id”:102793,“addtime”:“2009-01-15 01:07:19”,“点击”:675,“用户名”:“Vortarulo”,“性别” : “M”, “国家”: “德国”, “代码”: “德”, “langname声明”: “德国”, “pathmp3”: “http://apifree.forvo.com/audio/2b2g1f312k2n212p2o1m2a2j3l2j232j271i1b323n3i21262d1o313k2h283d3n213n1f273f3b34311h3b333o2k2g2q1h2o1o26342a2g3d3o1n2p2q241g212l282j221h3q3c3k3e3a1f313i2n212g2m2n2l2p3i222d2h1t1t_2l3n1i3g222e213e262727253g1l353j3n1l3e2m2f371t1t”, “pathogg”:“{{3} }”, “速率”:1, “NUM_VOTES”:1, “num_positive_votes”:1}]}
如果我直接通过获取请求:
http://apifree.forvo.com/key/b1b0641fac44c20e42373e0af5d05325/format/json/callback/pronounce/action/standard-pronunciation/word/am/language/ga
通过我的浏览器,我收到了预期的回复:
{“items”:[{“id”:101433,“addtime”:“2009-01-14 13:02:54”,“hits”:174,“username”:“generalbelly”,“sex” : “M”, “国”: “爱尔兰”, “代码”: “GA”, “langname声明”: “爱尔兰”, “pathmp3”: “http://apifree.forvo.com/audio/3d3j2o2c311k2k383b332c211m3d271o32273i211b3a252j3e3m2h3c1m3b2c2o372c3o333c311p25331n2l1n3p252o3j1i392i2c3832372o3d1i2e32371o2m2m2j2g3e3o1i2g2o211m281b2g243b3q2826221l2i1k2h1t1t_251o293n2m262p2k3l3g2q3q3i1f32222j3e292i352h1t1t”,” pathogg “:” http://apifree.forvo.com/audio/3535332936243o27341o21353h2q1o2m2k1h2g263c39381h1n2o2d1b3m252125212c3o3e2a2o3o1i383n283g223l343h2m372f253a3d262g352n3h3q331l3a3h3q2q1k1k1n273p1n2n2q27393j353q3o243p3f353o2h1t1t_1j1k3k342q3b1g2934362q2q2a3n2o3g3f2c3h1k1i2h1t1t”, “速率”: “0”, “NUM_VOTES”: “0”, “num_positive_votes”:0}]}
问题是什么?
答案 0 :(得分:2)
问题是$.ajax
将?callback=pronounce&_=1374366382184
附加到网址的末尾,然后会为您提供与预期不同的响应。你不希望这样。
您需要以下内容:
$.ajax({
url: url,
jsonpCallback: "pronounce",
jsonp:false, // prevent jquery from appending '?callback=pronounce'
dataType: "jsonp",
cache:true, // prevent '_=TIMESTAMP' from being appended to the url.
success: function (json) {
console.log(JSON.stringify(json));
var mp3 = json.items[0].pathmp3;
var ogg = json.items[0].pathogg;
var sound = '<audio autoplay="false"><source src='+mp3+'</source><source src='+ogg+'</source> </audio>';
$('body').append(sound);
},
error: function(){
console.log("error");
}
});
小提琴:http://jsfiddle.net/t4j7a/ - 打开你的控制台以查看响应。