2011-05-23 42 views

回答

1

是的。您可以使用Json並執行POST。如果您使用jQuery,則可以使用$ .ajax將值發佈到服務器端。希望這可以幫助。

+0

你能告訴我更多關於JSON(解釋,例如)? – Badr 2011-05-23 06:01:25

+1

這是一個很好的參考點http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx – ysrb 2011-05-23 06:11:47

3

是的。一種方法可能是,使用網絡方法;例如:

  1. 創建服務
  2. 從JavaScript方法,如呼叫:DataService.Push(yourObject);

例如:

JavaScript方法:

function btnGenerate_onclick(result) { 
    DataService.Push(getDataFromSomeDiv(), onGenerateReportComplete /*callback method*/); 
    //or 
    //DataService.Push(document.getElementById("myDiv").innerHTML, onGenerateReportComplete /*callback method*/); 

} 

function onGenerateReportComplete(result) { 
      alert("Success:" + result); 
} 

服務方法:

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.Web.Script.Services.ScriptService] 
public class DataService : System.Web.Services.WebService 
{ 

    [WebMethod(EnableSession = true)] //If you want? 
    public bool Push(object someObject) 
    { 
     //var v = someObject as MyObjectClass;//Process object 
     return true; 
    } 
} 

編輯:的javascript怎麼會知道什麼是服務器端的DataService ?

這需要在標記中引用Web服務。比如如下:

<asp:ScriptManager ID="sm" runat="server"> 
      <Services> 
      <asp:ServiceReference Path="DataService.asmx" /> 
     </Services> 
</asp:ScriptManager> 

或者您可以使用回調/ page methods

+0

你錯過了一些東西。 javascript如何知道服務器端的DataService是什麼? – VikciaR 2011-05-23 06:19:27

+0

好問題,請參閱編輯。 – 2011-05-23 06:37:17

2

不一樣。您可以將該對象序列化爲一個字符串,將該字符串發送給ASP.NET,然後再將其轉換爲另一側的對象。

JSON是一個很好的序列化格式,你可以將簡單的對象直接到是圍繞它(這是listed in the penultimate section of the JSON homepage)的各種庫。

對於更復雜的對象,您需要先提取需要重新創建數據的相關位數據。

4

在ASP.NET的WebForms我會用一個ScriptService:

結帳這個樣本: ASP.NET ScriptService deserialization problem with derived types

http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

GenerateScriptType屬性可以,如果你想通過/讓孔對象服務使用

[WebService(Namespace = "http://msdnmagazine.com/ws")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[GenerateScriptType(typeof(Object1))] 
[GenerateScriptType(typeof(Object2))] 
[ScriptService] 
public class StockQuoteService : WebService 
{ 
    static Random _rand = new Random(Environment.TickCount); 

    [WebMethod] 
    public int GetStockQuote(string symbol) 
    { 
     return _rand.Next(0, 120); 
    } 
} 
+0

您的示例顯示如何調用Web服務並傳遞一個值,但它不顯示如何傳遞對象(包括屬性)。 – 2011-05-23 06:47:18

1

Ben Dotnet在asp.net WebForms中正確使用ScriptService。除了使用ScriptService裝飾器GenerateScriptType裝飾器對於確保包含您想要使用的複雜類型很重要。我發現Ben鏈接的文章除了這個之外還有用:http://www.webreference.com/programming/asp-net-ajax/complex-data-types/index.html

下面是我如何能夠完成你想要的。首先,我定義了我想在代碼隱藏文件中使用的自定義類型。

namespace TestProject 
{ 
    public class SampleData 
     { 
      public int id { get; set; } 
      public string StartDate { get; set; } 
      public string EndDate { get; set; } 

      public SampleData() 
      { } 
    } 
    public partial class SamplePage : System.Web.UI.Page 
    { 
     /* The rest of the SamplePage.aspx.cs file goes here */ 
    } 
} 

然後,我在我的SamplePage代碼中創建一個WebMethod/ScriptMethod背後是這樣的:

[WebMethod] 
[ScriptMethod] 
[GenerateScriptType(typeof(SampleData))] 
public static bool EditReminder(SampleData data) 
{ 
    /* Server side code goes here */ 
    return true; 
} 

之後,客戶端頁面上,我能夠創造型的sampleData和傳球的對象,像這樣使用PageMethods。 不要忘記包含命名空間,這是必要的。

function some_javascript_function() { 
    var sample_data = new TestProject.SampleData() 
    sample_data.id = 1; 
    sample_data.StartDate = '6/24/1976'; 
    sample_data.EndDate = '3/20/2012'; 
    PageMethods.EditReminder(sample_data, OnEditReminderComplete) 
} 

function OnEditReminderComplete() { 
    if (result) alert("Success!"); 
    else alert("Failure!"); 
} 

而且,不要忘了包括腳本經理和啓用頁面的方法,如在某個地方,你的網頁上:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" /> 
相關問題