2016-06-13 151 views
0

我想將文本框數據轉換爲JSON格式,然後使用post方法將該JSON數據發佈到wcf rest服務?WCF Rest服務接受Json數據

+1

[當問一個關於由你的代碼有問題的問題,你會得到更好的答案,如果你提供的代碼的人可以用它來重現該問題](http://stackoverflow.com/help/mcve) – swiftBoy

+0

你r正確....但我不知道如何繼續 –

+0

爲此目的使用jQuery的AJAX調用wcf與json數據 –

回答

0

簡單的把你的aspx頁面上使用此代碼,並使用正確的服務URL

var WCF_Service_URL = "http://your_service_ip/Service_name.svc/YourMethod"; 
    var myData = $('txtMyTextBox').val(); 
    $.ajax({ 
     type: "POST", 
     url: WCF_Service_URL, 
     data: JSON.stringify(myData), // here convert into json format 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     processdata: true, 
     success: function (msg) { 
      alert(msg); 
     }, 
     error: function (ex) { 
      alert(ex); 
     } 
    }); 



    // wcf methods 
      // 1) your IServices.cs interface method 
      [OperationContract] 
      [WebInvoke(
      ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST", 
      UriTemplate = "YourMethod")] 
      string YourMethod(string JsonData); 

      // 2) your Services.cs 

       public string YourMethod(string JsonData) 
     { 

      string FirstName = JsonData.Firstname; // your json object data 

        // save here into database 

      return "Save successfully"; 

     } 
+0

其實我必須將表單數據轉換成json格式,然後我必須將這些轉換後的json數據發送到wcf服務....請好好回覆一步步驟過程 –

+0

確定上述行JSON.stringify(myData )將您的數據轉換爲json格式,並將此json類型的數據分步執行wcf –

+0

.... –