无法在JavaScript中获取查询字符串

时间:2013-12-06 05:17:38

标签: javascript web-services

我们为项目提供了函数getQueryStringVariableByItemID,并使用函数getData将一个Web服务用于游戏表中的游戏细节。我们相信getData部分工作正常,因为我们在另一个页面上使用类似的POST。 getQueryStringVariableByItemID是否正确抓取查询字符串?

我们使用html的body标签调用getData作为onload =“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>

1 个答案:

答案 0 :(得分:0)

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