jQuery ASMX Web服务调用-发送/接收复杂类型 [英] Jquery ASMX Web Service call - send / receive complex types

查看:108
本文介绍了jQuery ASMX Web服务调用-发送/接收复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定昨晚只是一天中的时间,缺少咖啡或放纵糖分.除此之外,我正在尝试使它正常工作.我不想更改/修改/添加新的Web服务方法.

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.

我有一个asmx Web服务:

I have an asmx web service:

public UserLogin Login(UserLogin p_objUserLogin)
{
}

我需要挂钩一个JQuery ajax调用. UserLogin对象并不那么复杂:

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

UserDetails对象很大,并且包含很多数据. (希望我不需要构建整个对象树来使它起作用).

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

我正在使用的脚本:

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.

推荐答案

确保您的Web服务类和方法经过修饰以处理传入的Ajax/json请求:

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

我不熟悉您用来配置有效负载对象的表示法(p_objUserLogin ['ClientCodeID'] ="Test";).我通常使用略有不同的表示法:

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';

但是,这可能是一个红色的鲱鱼-我不是JS对象专家,所以您的表示法可能是完全正确的.

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

最后,我不确定JS是否会自动将您的对象转换为JSON(var DTO = {'p_objUserLogin':p_objUserLogin};).我使用 json2库将JS对象显式序列化为JSON:

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.

这篇关于jQuery ASMX Web服务调用-发送/接收复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆