【问题标题】:Sending Form Parameters to Web-service using Operations使用操作将表单参数发送到 Web 服务
【发布时间】:2011-08-11 02:10:34
【问题描述】:

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

// 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# 翻译中

【问题讨论】:

    标签: c# javascript ajax web-services json


    【解决方案1】:

    您的 javascript 对象的第一个 'head' id NewSubscriber 必须与您的 Web 方法签名匹配,例如: 你打电话给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;
    }
    

    所以对象匹配并且名称匹配。

    【讨论】:

    • 知道了...完全理解,但我无法掌握的部分是一旦我进入:[WebMethod] public string AddDigitalSubscriber(NewSubscriber NewSubscriber) { string status = string.Empty; // Add Subscriber... return status; } 如何将 NewSubscriber.Email 设置为 web 服务中的电子邮件参数?
    • 它还给了我一个警告,说“找不到类型或命名空间名称NewSubscriber...
    • 如何使用 Reference.cs 文件?它似乎拥有我需要的一切?我还需要将其上传到我的网络服务器吗?
    猜你喜欢
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多