使用的环境:ASP.NET,jQuery
我有以下AJAX调用:
var tempVar = JSON.stringify({plotID:currentId});
$.ajax({
type: "POST",
url: "testPage.aspx/getPlotConfig",
data: tempVar,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#xDimLbl').text(msg.xDim);
$('#bDimLbl').text(msg.bDim);
}
});
后面的代码将getPlotConfig(字符串plotID)方法定义为
public static string getPlotConfig(string plotID)
{
string x = "T1";
string b = "T2";
return Json(new { xDim= x, bDim= b });
}
问题:
欢呼声
答案 0 :(得分:2)
这可能是指ASP.NET MVC控制器中使用的Json方法。由于您的getPlotConfig
函数是静态的,因此无法使用此方法。你可以看一下PageMethods。这是一个例子:
[WebMethod]
[ScriptMethod]
public static object getPlotConfig(string plotID)
{
var hash = new Dictionary<string, string>()
{
{ "key1", "valueA,valueB" },
{ "key2", "valueC,valueD" },
};
var x = "T1";
var b = "T2";
return new { xDim = x, bDim = b, hash = hash };
}
在javascript中:
success: function(msg) {
$('#xDimLbl').text(msg.d.xDim);
$('#bDimLbl').text(msg.d.bDim);
for(var key in msg.d.hash) {
var value = msg.d.hash[key];
// Do something with key and value...
}
}