我有一页,URL中包含查询字符串值。
QSecID = 164&QTempId = 55&QSecName = New%20Temp%20Bt
当页面加载它们并尝试获取值时,它可以正常工作。
$(document).ready(function() {
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
var urlName = getUrlParameter('QSecName');
alert(urlName);
getImageData(urlName); //Pass Query String Value to Function
});
现在,我将此值传递给C#,ASPX.CS页,并尝试基于QSecName
获取数据。但是我有错误。这是我的Jquery函数。
function getImageData(name) {
// alert(nextDay);
$.ajax({
type: "POST",
url: "Section.aspx/GetImageData",
//data: '',
data: JSON.stringify({
"dataSecName": name
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert("Success");
alert(JSON.parse(data.d));
}
});
}
我的C#页面在Jquery中返回数据集。
[WebMethod()]
public static string GetImageData(string dataSecName)
//public static string GetRole()
{
clsSection objSectNew = new clsSection();
DataSet DS = new DataSet();
DS = objSectNew.GetImageDataByJQ(dataSecName);
return JsonConvert.SerializeObject(DS);
}
编辑代码1这是我的SQL语句,当我运行页面和
时将执行该语句DS = objSectNew.GetImageDataByJQ(dataSecName);
此方法将查询字符串值传入以执行存储过程。
select mhp.ImageName,mhp.HotSpotID,imgdetail.XCordinate,imgdetail.YCordinate
from tbl_SOAPDetailsInfo e inner join M_ImageHotSpot mhp on e.ImgHotSpotName=mhp.ImgHotSpotNameByUser
inner join M_ImageHotSpotDetail imgdetail on mhp.HotSpotID=imgdetail.HotspotIDFK where e.SOAP_D_Name='New Temp Bt'
我想使用我的XCordinate
,YCordinate
和ImageName
使用jquery显示图像。但在警告框内
**[object] [object]**
错误显示。如何获取和分配该值X和Y并显示在DIV中。
因此根据您的问题,[object] [object]
不是错误。这意味着您不能以正确的方式获取它。
尽管我不确定从data
中的后端代码发送什么样的数据,但是您可以尝试以下方式,
var data = '{"XCordinate":99.5, "YCordinate":42.6, "ImageName":"YourImageName"}';
var obj = JSON.parse(data);
console.log(obj.XCordinate);
// expected output: 99.5
console.log(obj.YCordinate);
// expected output: 42.6
console.log(obj.ImageName);
// expected output: YourImageName
所以现在,如果您将代码替换为以上示例,则代码应如下所示:
function getImageData(name) {
// alert(nextDay);
$.ajax({
type: "POST",
url: "Section.aspx/GetImageData",
data: JSON.stringify({
"dataSecName": name
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var obj = JSON.parse(data);
console.log(obj.XCordinate);
// expected output: XCordinate value
console.log(obj.YCordinate);
// expected output: YCordinate value
console.log(obj.ImageName);
// expected output: ImageName value
}
});
}
注意:尝试在浏览器
console
中进行调试,您将了解对象符号的更多详细信息。
希望这会有所帮助