2017-07-19 67 views
0
http://localhost:51238/RestService.svc/FOSLoadingS1Opening?Data=[ 
{ 
"Name":"Sachin", 
"City":"Bengalueu", 
"FimStatus":"false", 
"Deno1":"50", 
"Deno2":"100", 
"Deno3":"500", 
"Deno4":"2000", 
"IndtVal1":"2500", 
"IndtVal2":"5000" }] 

我能夠通過查詢字符串傳遞json字符串。但是,當我想要傳遞無查詢字符串時,我收到錯誤。如何使用WCF在UriTemplate中傳遞JSON字符串而不使用查詢字符串

http://localhost:51238/RestService.svc/FOSLoadingS1Opening/[ 
{ 
"Name":"Sachin", 
"City":"Bengalueu", 
"FimStatus":"false", 
"Deno1":"50", 
"Deno2":"100", 
"Deno3":"500", 
"Deno4":"2000", 
"IndtVal1":"2500", 
"IndtVal2":"5000" }] 

當我通過上面的URL我得到錯誤「錯誤的請求」。

我想傳遞json字符串而不使用查詢字符串。請建議如何實現。

+1

唯一的方法是使用webinvoke(post)而不是webget –

回答

0

您必須使用WebInvoke和POST方法。

[OperationContract] 
[WebInvoke(Method = "POST", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "SaveData/{id}")] 
string SaveData(YourType typeObj); 

現在從客戶端構建對象並通過ajax調用發送json字符串化數據。

var obj = { 
     "Name":"Sachin", 
     "City":"Bengalueu", 
     "FimStatus":"false", 
     ... 
    }; 
    $.ajax({ 
     type: "POST", 
     url: "http://localhost/wcf.svc/SaveData/0", 
     data: JSON.stringify(obj), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     processData: true, 
     success: function (data, status, jqXHR) { 
      alert("success..." + data); 
     }, 
     error: function (xhr) { 
      alert(xhr.responseText); 
     } 
    });