我知道此论坛上存在多个类似下面的查询,但我无法找到我的案例中究竟出现的错误。
$.ajax({
type: "POST",
url: "default.aspx/GetMaturityValues",
data: jsonParams,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
alert(response.d);
}
});
[System.Web.Services.WebMethod]
public static string GetMaturityValues(string countryIDList, string xAxis, string yAxis, string bubbleSize)
{
//some code
}
执行不会流入C#代码。
jsonParams:
var paramList = '';
var countryIDList = '1,2,3,5';
var xAxis = '1';
var yAxis = '2';
var bubbleSize = '6';
paramList += 'countryIDList' + '":"' + countryIDList;
paramList += 'xAxis' + '":"' + xAxis;
paramList += 'yAxis' + '":"' + yAxis;
paramList += 'bubbleSize' + '":"' + countryIDList;
paramList = '{' + paramList + '}';
var jsonParams = JSON.stringify(paramList);
答案 0 :(得分:4)
尝试使用模型:
public class MyModel
{
public IList<int> CountryIDList { get; set; }
public int XAxis { get; set; }
public int YAxis { get; set; }
public int BubbleSize { get; set; }
}
您的WebMethod将作为参数:
[System.Web.Services.WebMethod]
public static string GetMaturityValues(MyModel model)
{
//some code
}
最后在客户端:
var paramList = {
countryIDList: [1, 2, 3, 5],
xAxis: 1,
yAxis: 2,
bubbleSize: 6
};
var jsonParams = JSON.stringify({ model: paramList});
$.ajax({
type: "POST",
url: "default.aspx/GetMaturityValues",
data: jsonParams,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
alert(response.d);
}
});
在这个例子中,所有的参数都是整数,你当然可以使用像字符串这样的其他数据类型:
public class MyModel
{
...
public string Foo { get; set; }
}
可以相应地从客户端传递为字符串:
var paramList = {
...
foo: 'bar'
};
答案 1 :(得分:2)
我认为问题只是发布时参数格式不正确。试试这个简单的解决方法:
<script type="text/javascript">
var params = {};
params.countryIDList = "test";
params.xAxis = "x";
params.yAxis = "y";
params.bubbleSize = "y";
$.ajax({
type: "POST",
url: "Test1.aspx/GetMaturityValues",
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
alert(response.d);
}
});
</script>
@DarinDimitrov(当然)是正确的,你可以从制作模型类中受益......但是你也可以使用sting参数。