可以't在JavaScript中抓取查询字符串

Can't Grab Query String in JavaScript

本文关键字:抓取 查询 字符串 JavaScript 可以      更新时间:2024-03-25

我们为我们的项目提供了函数getQueryStringVariableByItemID,并使用函数getData使用web服务从游戏表中获取游戏的详细信息。我们相信getData部分运行良好,因为我们在另一个页面上使用了类似的POST。getQueryStringVariableByItemID是否未正确获取查询字符串?

我们使用html的body标记onload="getData()"来调用getData。非常感谢!

代码:

<script type="text/javascript">
        function getQueryStringVariableByItemID(ItemID) {
            //use this function by passing it the name of the variable in the query
            //string your are looking for.  For example, if I had the query string
            //"...?id=1" then I could pass the name "id" to this procedure to retrieve
            //the value of the id variable from the querystring, in this case "1".
            ItemID = ItemID.replace(/['[]/, "'''[").replace(/[']]/, "''']");
            var regexS = "[''?&]" + ItemID + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(window.location.search);
            if (results == null)
                return "";
            else
                return decodeURIComponent(results[1].replace(/'+/g, " "));
        }
        function getData() {
            var ItemID = getQueryStringVariableByItemID(ItemID)
            $.ajax({
                type: "POST",
                url: "./WebServiceTry.asmx/GetGameDetails",
                data: "{'ItemID': '" + escape(ItemID) + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var data = response.d;
                    $('#output').empty();
                    $.each(data, function (index, item) {
                        var Title = item.Title
                        var Price = "$" + item.Price
                        var Year = "Year: " + item.Year
                        var Developer = "Developer: " + item.Developer
                        var Platform = "Platform: " + item.Platform
                        $('#output').append('<li>' + Title + '</li>');
                        $('#output').append('<li>' + Price + '</li>');
                        $('#output').append('<li>' + Year + '</li>');
                        $('#output').append('<li>' + Developer + '</li>');
                        $('#output').append('<li>' + Platform + '</li>');
                        $('#output').listview('refresh');
                    });
                },
                failure: function (msg) {
                    $('#output').text(msg);
                }
            });
    }
</script>

在getData中传递的ItemID(在getData内部调用时)应该是未定义的,因为函数没有该变量。传递一个有效的id,它将正常工作。