在Webmethod中接收jsonQuery字符串 [英] receive the jsonQuery String in Webmethod

查看:242
本文介绍了在Webmethod中接收jsonQuery字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将Json发送到Web方法.网络方法未将json作为字符串接收.

I have send a Json to a web method. While web method not receiving the json as a string.

POST方法Test1.aspx-

 var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:49633/Test3.aspx/Get");

                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"d\":{\"accessKey\":\"Chennai\",\"channelId\":\"1025\"}}";

                    streamWriter.Write(json);
                }
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var responseText = streamReader.ReadToEnd();
                    return responseText;
                }

Test3.aspx中的WEB方法

   [System.Web.Services.WebMethod]
        public static string Get(string d)
        {
            return d;
        }

响应-

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title>

    </title></head>
    <body>
        <form name="form1" method="post" action="Booking.aspx" id="form1">
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTg3NjI4NzkzNmRkOvsswBe8G74mfKP2QBvs0WW2jms=" />

        <div>   
  <span id="Label3"><font color="Fuchsia">Responce:-</font></span>
            <span id="Label1">Label</span> 
        </div>
        </form>
    </body>
    </html>

查询-

我收到HTML页面的回复.但是我想接收通过POST方法发送的Json

I receive a response as HTML Page. But I want to Receive the Json Which I was Send via POST method

JSON STRING-

{"d":{"accessKey":"Chennai","channelId":"1025"}}   

编辑

我已将json字符串更改为{"accessKey":"Chennai","channelId":"1025"},并更改了web方法来获取字符串.

I have changed the json string as {"accessKey":"Chennai","channelId":"1025"} and changed the webmethod to get the string.

 [System.Web.Services.WebMethod]
            public static string Get(string accessKey, string channelId)
            {
                return accessKey + channelId;
            }  

我收到了正确的channelId,accesskey值.但是我原来的弦很大.因此,我需要接收通过POST方法发送的同一Json字符串. bzs我只在接受部分.一旦我的客户通过调用此Websmethod发送Json字符串.谢谢.

I have received a correct channelId, accesskey values. But my original string is very big. so I need to receive same Json String which i send via POST method. bzs I am doing receiving part only. Once of my Client Send the Json String via Calling this Websmethod. Thanks.

编辑2-

根据梅兹·赛义德(Mez Said)的介绍,我已经包含ScriptMethod.但是它通过远程服务器返回了一个错误:(500)Internal Server Error.

As per Mez Said I have include the ScriptMethod. But It through The remote server returned an error: (500) Internal Server Error.

 [System.Web.Services.WebMethod]

    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public static string Get(string d)
            {          
                return d;
            }

推荐答案

可能达到了json的最大限制.尝试将其添加到您的web.config.

It is possible you reached the max limit for json. Try adding this to your web.config.

<configuration>
    ... 
    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="300000" />
            </webServices>
        </scripting>
    </system.web.extensions> 
</configuration> 

这篇关于在Webmethod中接收jsonQuery字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆