2013-04-15 114 views
0

使用這種服務,我想使用senchatouch2..Service POST方法被寫入(WCF)發送陣列使用POST方法參數在senchatouch2

服務聲明數組值存儲到數據庫中:

[OperationContract] 
[WebInvoke(Method = "POST", 
      ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      UriTemplate = "/Check1")] 
int Psngr(string[] FirstName); 

服務定義:

public static int Psngr(string[] FirstName) 
{ 
    List<Psgr> psgr = new List<Psgr>(); 
    var getVal = from s in FirstName select s; 
    int count = getVal.Count(); 

    SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["db"].ToString()); 
    con.Open(); 

    using (var cmd = new SqlCommand("SP_InsertCheck1", con)) 
    { 
     int result; 
     cmd.CommandType = CommandType.StoredProcedure; 

     for (int i = 0; i < count; i++) 
     { 
      cmd.Parameters.Clear(); 
      cmd.Parameters.AddWithValue("@FirstName", FirstName[i]); 

      using (var Da = new SqlDataAdapter(cmd)) 
      using (var Ds = new DataSet()) 
      { 
       Da.Fill(Ds); 
       result = Convert.ToInt16(Ds.Tables[0].Rows[0]["Result"].ToString()); 
      } 
     } 
     return 1; 
    } 
} 

我accesssed通過我的ajax請求該服務如下:

Ext.Ajax.request({ 
    url:'http://ws.easy4booking.com/E4b.svc/Check1',             
    method:'POST',            
    disableCaching: false,           
    headers: { 
     'Accept': 'application/json', 
     'Content-Type': 'application/json' 
    }, 
    params: { 
     FirstName:fname_toString, //FirstName:["Sam","Paul"], 
}, 
    success:function(response) { 
     console.log(response); 
    } 
}); 

當我訪問該服務LIK上面提到我得到了以下錯誤

請求錯誤:

The server encountered an error processing the request. The exception message is 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Psngr'. Encountered unexpected character 'F'.'. See server logs for more details. The exception stack trace is:

at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

+1

我要說的是,你的原始請求的樣子監控。使用Fiddler或WireShark等工具查看原始請求。您也可以啓用對WCF服務的跟蹤並查看造成問題的原因。 – Rajesh

+0

我不明白在哪裏的確切問題.. sencha代碼或服務中的問題?好心回覆我 – Annie

+0

啓用wcf跟蹤,使用svcTraceViewer.exe查看它們IMO - 這將有助於找到錯誤來自哪裏。 – evgenyl

回答

1

當V使用POST方法傳遞在SenchaTouch2參數使用jsonData在AJAX請求一樣,

Ext.Ajax.request({ url:'', method:'POST', disableCaching:false, headers: { 'Accept':'application/json', 'Content-Type':'application/json' }, jsonData: { FirstName:fname //{"FirstName":["Sam","paul"]} }, success: function(response) { console.log(response.responseText); }, failure: function(response) { console.log(response.responseText); }, });

0

當你的客戶端執行的請求到WCF Rest服務的原始請求應該如下所示:

POST http://localhost/SampleService/RestService/Check1 HTTP/1.1 
User-Agent: Fiddler 
Content-Type: application/json 
Host: localhost 
Content-Length: 33 

{"FirstName":["Sam","paul"]} 

似乎你的sencha代碼正在傳遞WCF無法反序列化的字符串[]。你可能會考慮把它作爲jsonData而不是param來傳遞。

也期待在這個link這可能會有所幫助。(他們正試圖使用​​該服務是在Spring MVC,但我想這應該是WCF服務,以及相同)

+0

我改變了參數jsonData,現在其工作正常..非常感謝你的回覆 – Annie

+0

對於GET方法,我們應該使用params和POST方法,我們應該使用jsonData呵呵? – Annie

+0

@安妮:如果您可以將其標記爲答案,那麼它會很好,以便對出現同樣問題的人有所幫助 – Rajesh