jqueryajax不调用C#Webmethod
我知道在这个论坛上存在多个类似下面的问题,但我找不到我的问题的确切原因jqueryajax不调用C#Webmethod,c#,jquery,ajax,C#,Jquery,Ajax,我知道在这个论坛上存在多个类似下面的问题,但我找不到我的问题的确切原因 $.ajax({ type: "POST", url: "default.aspx/GetMaturityValues", data: jsonParams, contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert
$.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]
公共静态字符串GetMaturityValues(字符串countryIDList、字符串xAxis、字符串yAxis、字符串bubbleSize)
{
//一些代码
}
执行没有流入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);
尝试使用模型:
public class MyModel
{
public IList<int> CountryIDList { get; set; }
public int XAxis { get; set; }
public int YAxis { get; set; }
public int BubbleSize { get; set; }
}
最后,关于客户:
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'
};
我认为问题很简单,就是在发布时参数的格式不正确。尝试以下简单的解决方法:
<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>
var params={};
params.countryIDList=“测试”;
params.xAxis=“x”;
params.yAxis=“y”;
params.bubbleSize=“y”;
$.ajax({
类型:“POST”,
url:“Test1.aspx/GetMaturityValues”,
数据:JSON.stringify(params),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(响应){
警报(响应);
},
故障:功能(响应){
警报(response.d);
}
});
@DarinDimitrov(当然)是正确的,您可以从创建模型类中获益。。。但是你也可以只使用sting参数就可以了。不会流入C代码中,这到底是什么意思?是否要将ajax转换为C#?ajax代码没有调用预期的方法。失败响应中得到了什么?显示浏览器调试器中显示的
jsonParams
错误响应是什么?需要是var jsonParams=JSON.stringify({“model”:paramList})
为了使参数绑定在使用WebMethod
时起作用,如果您是正确的,我将更新我的答案以反映您的评论。
<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>