在senchatouch2中使用POST方法将数组作为参数发送

时间:2013-04-15 10:27:48

标签: arrays wcf post parameters

使用此服务我想在senchatouch2中使用POST方法将数组值存储到数据库中.Service写在(WCF)

服务声明:

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

我通过我的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);
    }
});

当我访问上面提到的这个服务时,我得到了以下错误

请求错误:

  

服务器在处理请求时遇到错误。异常消息是'格式化程序在尝试反序列化消息时引发异常:反序列化操作'Psngr'的请求消息正文时出错。遇到意想不到的字符'F'。'。请参阅服务器日志以获取更多详异常堆栈跟踪是:

     

at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message,Object [] parameters)at at   System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息消息,Object []参数)at at   System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []参数)at at   System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,Object []参数)at at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)at at   System.ServiceModel.Dispatcher.MessageRpc.Process中的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)(布尔值   isOperationContextSet)

2 个答案:

答案 0 :(得分:1)

当v使用POST方法在SenchaTouch2中传递参数时,在Ajax请求中使用 jsonData

  

Ext.Ajax.request({       网址: '',       方法: 'POST',       disableCaching:假的,       标题:{               '接受': '应用/ JSON',               “内容类型”:“应用/ JSON”       },        jsonData :{                 FirstName:fname // {“FirstName”:[“Sam”,“paul”]}       },       成功:功能(响应)       {        的console.log(response.responseText);       },       失败:功能(响应)       {        的console.log(response.responseText);       },   });

答案 1 :(得分: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服务也应该是相同的)