全局变量未在AJAX中分配

时间:2014-04-29 00:18:54

标签: javascript jquery ajax

为什么当前函数GetSongs(if)中的ajax语句循环后才更改值。我调试了,值在循环时不会改变值。

var currentT = "test";
    function GetSongs(id) {
        $.ajax(
        {
            type: "Get",
            url: "@Url.Action("GetSongs", "Game")",
            data: { playlistId : id },
            success: function (data) {
                json = data;
                var obj = JSON.parse(json);
                for (var i in obj) {
                    var videoId = youtube_parser(obj[i].SongURL);
                    getVideoInformation(videoId);
                    $("#song-table").append("<tr><td>" + currentT.toString() + "</td></tr>");
                    playlist.push(obj[i].SongURL);
                }

            }
        });
    }

    function youtube_parser(url) {
        var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
        var match = url.match(regExp);
        if (match && match[7].length == 11) {
            return match[7];
        } else {
            alert("Url incorrecta");
        }
    }

    function registerScript(url) {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.src = url;
        document.getElementsByTagName('head')[0].appendChild(s);
    }

    function videoInfoCallback(info) {
        if (info.error) {
            alert('Error\n\n' + info.error.message);
        } else {
            var message = info.data.title;
            currentT = message;
            return message;
        }
        return "Parser Error";
    }

function getVideoInformation(id) {
        return registerScript('https://gdata.youtube.com/feeds/api/videos/' + id + '?v=2&alt=jsonc&callback=videoInfoCallback');
}

0 个答案:

没有答案