如何将forvo API与jquery ajax一起使用?

时间:2013-01-23 22:45:31

标签: javascript jquery ajax api

我正在尝试这个:

   $("i.icon-play-circle").click(function(){
        var word = "你好";
        var url = 'http://apifree.forvo.com/key/XXXXXXXXXXXXXXXXXXXXXXXXXXX/format/json/action/standard-pronunciation/word/'+encodeURI(word)+'/language/zh';
        $.ajax({
            url: url,
            crossDomain: true,
            dataType: "jsonp",
            success: function (i,item) {
                alert(item);
            },
            error: function () {
                alert('foo');
            }
        });
    });

我在firebug控制台中遇到了这个错误:

SyntaxError: invalid label
[Break On This Error]   

{"items":[{"id":1967282,"addtime":"2013-01-21 23:35:31","hits

zh?cal...0549189 (line 1, col 1)

更新

如果你想回到jsonp,这里是如何格式化网址:

apifree.forvo.com/key/XXXXXXX/format/json/的回调/ your_function_here /动作/标准发音/字/你好/语言/ ZH

结果:

pronounce({"items":[{"id":436,"addtime":"2008-03-16 10:40:22","hits":4346,"username":"geneboy","sex":"m","country":"China","code":"zh","langname":"Mandarin Chinese","pathmp3":"http:\/\/apifree.forvo.com\/audio\/2i253j1l2e2m1b2i2f3q2d2g2f341k2q1f3h2g1b3m223i342b2q3k28323n2732372e1b1o393k2k1m1i2q3m353q253l36371l3k3a3q3d271h3133342o263g3223_3m2d2p2f2d1f2k3n1h3i3g2q263f293e1b253g1i2f2h1t1t","pathogg":"http:\/\/apifree.forvo.com\/audio\/3b2625373d3f2n1j233d2b211i3j3p3g1b1j1m312i3j2f261l2e2n383k371i1m3a3l292o3k2h29293h3q3c3f362h2k2h271m3i312o211h3m3g1h3e351m2n2d27_293q2c2l1m343a2q26343a391k1p3m1o2a2q333f35371t1t","rate":5,"num_votes":5,"num_positive_votes":5}]})

我现在需要帮助编写一个打印“pathmp3”值的函数。

1 个答案:

答案 0 :(得分:1)

以下是适用于我的ajax代码:

    word = "你好"
    var url = 'http://apifree.forvo.com/key/XXXXXXXXXXXXXXXX/format/json/callback/pronounce/action/standard-pronunciation/word/'+encodeURI(word)+'/language/zh';
    $.ajax({
        url: url,
        jsonpCallback: "pronounce",
        dataType: "jsonp",
        type: "jsonp",
        success: function (json) {
       var mp3 = json.items[0].pathmp3;
       var ogg = json.items[0].pathogg;
   },
    error: function(){
        console.log("error");
}});