Can';JavaScript中的t抓取查询字符串
我们为我们的项目提供了函数getQueryStringVariableByItemID,并且正在使用函数getData使用web服务从游戏表中获取游戏的详细信息。我们相信getData部分工作正常,因为我们在另一个页面上使用了类似的帖子。getQueryStringVariableByItemID是否未正确获取查询字符串 我们使用html的body标记onload=“getData()”调用getData。非常感谢 代码:Can';JavaScript中的t抓取查询字符串,javascript,web-services,Javascript,Web Services,我们为我们的项目提供了函数getQueryStringVariableByItemID,并且正在使用函数getData使用web服务从游戏表中获取游戏的详细信息。我们相信getData部分工作正常,因为我们在另一个页面上使用了类似的帖子。getQueryStringVariableByItemID是否未正确获取查询字符串 我们使用html的body标记onload=“getData()”调用getData。非常感谢 代码: 函数getQueryStringVariableByItemID(It
函数getQueryStringVariableByItemID(ItemID){
//通过在查询中传递变量的名称来使用此函数
//您正在查找的字符串。例如,如果我有查询字符串
//“…?id=1”,然后我可以将名称“id”传递给此过程以检索
//querystring中id变量的值,在本例中为“1”。
ItemID=ItemID.replace(/[\[]/,“\\\[”)。replace(/[\]]/,“\\\]”);
var regexS=“[\\?&]”+ItemID+“=([^&\\]*)”;
var regex=新的RegExp(regexS);
var results=regex.exec(window.location.search);
如果(结果==null)
返回“”;
其他的
返回组件(结果[1]。替换(/\+/g,”);
}
函数getData(){
var ItemID=getQueryStringVariableByItemID(ItemID)
$.ajax({
类型:“POST”,
url:“./WebServiceTry.asmx/GetGameDetails”,
数据:“{'ItemID':'”+转义(ItemID)+“}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(响应){
var数据=响应d;
$(“#输出”).empty();
$。每个(数据、功能(索引、项目){
var Title=项目名称
var Price=“$”+项目价格
var Year=“Year:+项目年份
var Developer=“开发者:”+项目开发者
var Platform=“平台:”+项目平台
$(“#输出”).append(“”+Title+” ”);
$(“#输出”)。追加(“”+价格+” );
$(“#输出”)。追加(“”+年份+” );
$(“#输出”).append(“”+Developer+” );
$(“#输出”).append(“”+Platform+” );
$('输出').listview('刷新');
});
},
故障:功能(msg){
$(“#输出”).text(msg);
}
});
}
您在getData中传递的ItemID(在getData内部调用时)应该是未定义的,因为函数没有该变量。传递一个有效的id,它将正常工作很抱歉,但我对javascript还是相当陌生的。那么我应该对getData代码做什么呢?你能告诉我方法调用的顺序吗?在getData()内部的getQueryStringVariableByItemID中传递的ItemId应该是未定义的,因为这个变量甚至不在任何地方使用。我们的方法调用顺序是getData()函数,然后是getQueryStringVariableByItemID函数。我们在项目中的单独页面上声明了ItemID,并试图使用getQueryString函数将该值从搜索页面传递到此页面,以显示所选结果的详细信息。我认为此函数中无法访问此ItemId。是否确定此处未定义ItemId?以访问您在其他页面中定义的内容(假设它在另一个文件/或另一个类中)您需要通过对象访问此变量,除非您在窗口变量中声明它。
<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>