How do i pass parameters from JQuery to ASP.NET webmethod?

那年仲夏 提交于 2019-12-02 09:31:39

问题


I have written this jQuery ajax method below which calls a webmethod. The call happens fine except the parameter which is a User object has empty fields. I can see the values in firebug when i debug but they do not get through to the User object parameter in the webmethod

There are two values i am trying to pass from the my jQuery method to the Webmethod which are "UserID" (Guid) and "About" (string) which are both properties of the User class, but on the service end, the User object is just empty. Please help me identify whatever i am missing. Thank you ... See code below.

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

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

回答1:


I think you are missing the single quotes in user when defining data. Please change data line with the code shown below:

data: "{'user':" + JSON.stringify(user) + "}",



回答2:


So I figured what i was doing wrong, both fields have private setter in the Domain object. therefore jQuery cant set the properties.

thanks all



来源:https://stackoverflow.com/questions/27758029/how-do-i-pass-parameters-from-jquery-to-asp-net-webmethod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!