2011-08-11 36 views
1

客戶端我試圖捕捉到這樣的領域:發送表單參數的Web服務使用Operations

// 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#翻譯

回答

0

您的JavaScript對象的第一個「頭部」的ID NewSubscriber必須在Web方法簽名匹配,例如: 你用var DTO = { 'NewSubscriber' : NewSubscriber };

調用url: "NewSubscriberService.asmx/AddDigitalSubscriber",所以,你需要的東西像這樣:

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

所以對象匹配和名稱匹配。

+0

明白了......完全明白,但我沒有把握的部分是,一旦我在:'[的WebMethod] 公共字符串AddDigitalSubscriber(NewSubscriber NewSubscriber) { 串狀態=的String.Empty; // Add Subscriber ... return status; }'我如何將NewSubscriber.Email設置爲web服務中的email參數? –

+0

它也給了我一個警告,說「類型或命名空間名稱'NewSubscriber'找不到... –

+0

@Devise,你看到編輯嗎? – Joe