使用操作将表单参数发送到Web服务

时间:2011-08-11 02:10:34

标签: c# javascript ajax web-services json

客户端我正在尝试捕获这样的字段:

// Initialize the object, before adding data to it.
//  { } is declarative shorthand for new Object().
var NewSubscriber = { };

NewSubscriber.FirstName = $("#FirstName").val();
NewSubscriber.LastName = $("#LastName").val();
NewSubscriber.Email = $("#Email").val();
NewSubscriber.subscriptionID = $("#subscriptionID").val();
NewSubscriberNewPerson.Password = "NewPassword1";
NewSubscriber.brokerID = "239432904812";

// Create a data transfer object (DTO) with the proper structure.
var DTO = { 'NewSubscriber' : NewSubscriber };

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "NewSubscriberService.asmx/AddDigitalSubscriber",
data: JSON.stringify(DTO),
dataType: "json"
});

现在这是我遇到的问题。如果更容易,我如何使用C#或vb.net发送这些参数并在Web服务中设置它们?非常感谢任何帮助

这是我到目前为止所做的:

public class Service1 : System.Web.Services.WebService
 {



    public SubNameSpace.WebServiceSubBook.drmProfile _profile;


    [WebMethod]
    public string SetProfileData (object DTO) {
        SetProfile (DTO);


    }
    [WebMethod]
    public class SetProfileData (SubNameSpace.WebServiceSubBook.drmProfile _profile;) {
        this._profile = _profile;


        return "success";
    }
 }
}

SetProfile是Web服务中的一项操作。 SetProfileRequest是操作中的消息。我想设置某些参数,然后在代码隐藏文件中列出其他参数,例如:

access_time = 30;

我完全迷失了......帮助!

前端编码器在C#翻译中丢失

1 个答案:

答案 0 :(得分:0)

您的javascript对象的第一个'head'标识NewSubscriber必须与您的网络方法签名相匹配,例如: 您使用url: "NewSubscriberService.asmx/AddDigitalSubscriber",

致电var DTO = { 'NewSubscriber' : NewSubscriber };

所以你需要这样的东西:

[WebMethod]
public string AddDigitalSubscriber(NewSubscriber NewSubscriber)
{
    string status = string.Empty;
    // Add Subscriber...
    string emailFromJavascript = NewSubscriber.Email;
    // do something
    return status;
}

您需要.NET中的类,如下所示,以匹配您的javascript对象:

//... Class in .NET
public class NewSubscriber
{
    public string FirstName;
    public string LastName;
    public string Email;
    public int subscriptionID;
    public string Password;
    public string brokerID;
}

所以对象匹配,名称匹配。