小编典典

如何将参数从JQuery传递到ASP.NET WebMethod?

ajax

我已经编写了这个jQuery
ajax方法,在下面调用了webmethod。除了作为用户对象的参数具有空字段之外,调用进行得很好。我在调试时可以在firebug中看到值,但它们无法通过webmethod中的User对象参数

我试图从jQuery方法传递给Webmethod的两个值是“ UserID”(Guid)和“
About”(字符串),它们都是User类的属性,但在服务端,是User对象只是空的。请帮助我确定我所缺少的。谢谢。请参见下面的代码。

jQuery查询

function updatePersonalProfile(userId) {
var user = {};
user.UserID = userId;
var updatedPersonalProfile = $(".txtPersonalProfile").html();
user.About = updatedPersonalProfile;
$.ajax({
    type: "POST",
    url: "PresentationService.asmx/updateUserPersonalProfile",
    dataType: "json",
    data: "{user:" + JSON.stringify(user) + "}",
    contentType: "application/json; charset=utf-8",
    success: function(response) {
    },
    error: function(response) {
        alert(response.d);
    }
});
}

网络方法

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void UpdateUserPersonalProfile(User user)
    {
        if (null == portfolioService)
        {
            portfolioService = new PortfolioService();
        }
        portfolioService.updateUserPersonalProfile(user);
    }

阅读 366

收藏
2020-07-26

共1个答案

小编典典

所以我发现我做错了什么,两个字段在Domain对象中都有私有设置程序。因此,jQuery无法设置属性。

谢谢大家

2020-07-26