Jquery ASMX Web Service call - send / receive complex types

大城市里の小女人 提交于 2020-01-06 17:34:37

问题


Not sure if it's just the time of day, lack of coffee or over indulgence of sugar from last night. Besides that I'm trying to get this working. I do not want to change / modify / add a new web service method.

I have an asmx web service:

public UserLogin Login(UserLogin p_objUserLogin)
{
}

I need to hook a JQuery ajax call up to that. The UserLogin object is not that complex:

public class UserLogin
{
    public UserLogin();

    public string Privileges { get; set; }
    public string ClientCodeID { get; set; }
    public UserDetails User { get; set; }
    public string UserLoginMessage { get; set; }
    public bool Valid { get; set; }
}

The UserDetails object is quite large, and includes a lot more data. (Hopefully I don't need to build the entire object tree to get this to work).

public class UserDetails
{
    public string CellPhone { get; set; }
    public string Email { get; set; }
    public string EncryptedPassword { get; set; }
    public string FirstName { get; set; }
    public string FullName { get; }
    public string Initials { get; set;
    public bool InService { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public byte[] Signature { get; set; }
    public string SimCard { get; set; }      
    public int UserID { get; set; }
    public string UserName { get; set; }
    public SecurityRole UserSecurityRole { get; set; }
    public Workgroup UserWorkgroup { get; set; }
}

The script I'm playing around with:

function CallService() {
    var p_objUserLogin = {};
    p_objUserLogin['ClientCodeID'] = "Test";
    var DTO = { 'p_objUserLogin': p_objUserLogin };
    $.ajax({
        type: "POST",
        url: "UtilityServices2006.asmx/Login",
        data: JSON.stringify(DTO),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: true,
        success: function (msg) {
            alert(msg);
        },
        error: function (req, status, error) {
            alert(req + ", " + status + ", " + error);
        },
        complete: function (req, status) {
            alert(req + ", " + status);
        }
    });

}

Any help would be quite amazing.


回答1:


Ensure your webservice class and method are decorated to handle incoming ajax/json requests:

[ScriptService]
public class MyService: WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public UserLogin Login(UserLogin p_objUserLogin)
    {
    }
}

I'm not familiar with the notation you're using to configure your payload object (p_objUserLogin['ClientCodeID'] = "Test";). I've usually used slightly different notation:

p_objUserLogin.ClientCodeID = 'Test';

However, that might be a red herring - I'm not a JS-objects expert, so your notation may be perfectly valid.

Finally, I'm not sure if JS will automatically convert your object to JSON (var DTO = { 'p_objUserLogin': p_objUserLogin };). I use the json2 library to explicitly serialize JS objects to JSON:

var DTO = { 'p_objUserLogin': JSON.stringify(p_objUserLogin) };

Hope this helps you nail down the issue.



来源:https://stackoverflow.com/questions/4073059/jquery-asmx-web-service-call-send-receive-complex-types

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