-1

以下小提琴没有给出预期的 json 回复:

http://jsfiddle.net/cpG2J/3/

相反,我得到:

{"items":[{"id":102793,"addtime":"2009-01-15 01:07:19","hits":675,"username":"Vortarulo","sex":"m ","country":"Germany","code":"de","langname":"German","pathmp3":" http://apifree.forvo.com/audio/2b2g1f312k2n212p2o1m2a2j3l2j232j271i1b323n3i21262d1o313k2h283d3n213n1f273f3b34311h3b333o2k2g2q1h2o1o26342a2g3d3o1n2p2q241g212l282j221h3q3c3k3e3a1f313i2n212g2m2n2l2p3i222d2h1t1t_2l3n1i3g222e213e262727253g1l353j3n1l3e2m2f371t1t ","pathogg": " http://apifree.forvo。com/audio/3a1l2n3d39221g2k352g3l1o2f312e2f2d3l252o1i3i2p322g252p281b3q3a1p3b2c1k3c2m3a341h3l1h2o1o2m213b1k243q2c3f361b1f1k3d313k2j2i3p3831393a2i2o3b3i2b1g3b3c392232253d3b2h1f1f1f2c371t1t_2g2o2d3l2138343k343c1b1f1l2b3b382i3h1o2h2p2h1t1t ","rate":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 ","country":"Ireland","code":"ga","langname":"Irish","pathmp3":"http://apifree.forvo.com/audio/3d3j2o2c311k2k383b332c211m3d271o32273i211b3a252j3e3m2h3c1m3b2c2o372c3o333c311p25331n2l1n3p252o3j1i392i2c3832372o3d1i2e32371o2m2m2j2g3e3o1i2g2o211m281b2g243b3q2826221l2i1k2h1t1t_251o293n2m262p2k3l3g2q3q3i1f32222j3e292i352h1t1t","pathogg": “http://apifree.forvo。com/audio/3535332936243o27341o21353h2q1o2m2k1h2g263c39381h1n2o2d1b3m252125212c3o3e2a2o3o1i383n283g223l343h2m372f253a3d262g352n3h3q331l3a3h3q2q1k1k1n273p1n2n2q27393j353q3o243p3f353o2h1t1t_1j1k3k342q3b1g2934362q2q2a3n2o3g3f2c3h1k1i2h1t1t","rate":"0","num_votes":"0","num_positive_votes":0}]}

问题是什么?

4

1 回答 1

2

问题在于$.ajax附加?callback=pronounce&_=1374366382184到 url 的末尾,然后它会为您提供与预期不同的响应。你不想要那个。

您需要以下内容:

$.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/ - 打开你的控制台查看响应。

于 2013-07-21T00:32:50.637 回答