简体   繁体   English

jQuery Ajax 不调用 C# Webmethod

[英]jQuery Ajax not invoking C# Webmethod

I know multiple queries like below exist on this forum but I could not found what exactly is wrong in my case.我知道这个论坛上存在多个类似下面的查询,但我找不到我的情况到底出了什么问题。

$.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   
}

The execution is not flowing into C# code.执行没有流入 C# 代码。

The jsonParams: 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);

Try using a model:尝试使用模型:

public class MyModel
{
    public IList<int> CountryIDList { get; set; } 
    public int XAxis { get; set; }
    public int YAxis { get; set; }
    public int BubbleSize { get; set; }
}

that your WebMethod will take as parameter:您的 WebMethod 将作为参数:

[System.Web.Services.WebMethod]
public static string GetMaturityValues(MyModel model)
{
    //some code   
}

and finally on the client:最后在客户端:

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);
    }
});

In this example all the parameters are integers, buy you could of course use other data types like strings for example:在这个例子中,所有参数都是整数,你当然可以使用其他数据类型,例如字符串:

public class MyModel
{
    ...
    public string Foo { get; set; }
}

which can be passes as string from the client accordingly:可以相应地从客户端作为字符串传递:

var paramList = {
    ...
    foo: 'bar'
};

I think the problem is simply that you parameters are not correctly formatted when you post.我认为问题只是您发布时参数格式不正确。 Try this simple workaround:试试这个简单的解决方法:

<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 is (of course) correct that you could benefit from making a model class... But you can make it work with just sting parameters as well. @DarinDimitrov 是(当然)正确的,您可以从创建模型类中受益...但是您也可以仅使用 sting 参数使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM