【问题标题】:jQuery/json read parameters from POST in .NET web servicejQuery/json 从 .NET Web 服务中的 POST 读取参数
【发布时间】:2011-01-14 14:55:15
【问题描述】:

我无法弄清楚如何从来自 jQuery 的 POST 中读取 json 字符串(我见过的大多数文章都展示了如何发送 json,但没有展示如何从网络收到的 POST 消息中获取它服务。

这是我目前写的代码。

在客户端:

<input type="text" id="UserNameText" />
<br />
<input type="password" id="PasswordText" />
<br />
<input type="button" id="Login" value="Login" onclick="DoLogin();" />

<script type="text/javascript" language="javascript">
    function DoLogin() 
    {
        var un = document.getElementById('UserNameText').value;
        var pw = document.getElementById('PasswordText').value;
        var info = "{ 'UserName':'" + un + "', 'Password':'" + pw + "'}";

        $.ajax(
        {
            type: "POST",
            url: "http://localhost:60876/Login.asmx/LoginSpecial",
            dataType: 'json',
            data: info,
            contentType: "application/json; charset=utf-8",
            success: function (msg) { alert(msg.d); },
            error: function (msg) { alert(msg.responseText); }
        });
    }
</script>

在服务器端我有这个:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string LoginSpecial()
{
    // none of these seem to be working
    NameValueCollection parameters = HttpContext.Current.Request.Params;
    string p = parameters["QUERY_STRING"] != null ? parameters["QUERY_STRING"].ToString() : String.Empty;
    string info = HttpContext.Current.Request["info"] != null ? HttpContext.Current.Request["info"].ToString() : String.Empty;
    string data = HttpContext.Current.Request["data"] != null ? HttpContext.Current.Request["data"].ToString() : String.Empty;

    // test json string, need to read from the jquery post
    string json = "{ 'UserName':'test', 'Password':'test'}";

    // the following two lines of code work ok with the test json string above.
    JavaScriptSerializer serial = new JavaScriptSerializer();
    Credentials credential = (Credentials)serial.Deserialize(json, typeof(Credentials));

    return "Some json message here";
}

我已经设置了断点,并且按预期访问了 Web 服务,但我不知道如何从 POST 消息中获取 json 字符串。

【问题讨论】:

    标签: jquery web-services json parameters


    【解决方案1】:

    如果您希望您的 Web 服务方法与提交的 JSON 对象一起使用,您必须将您的方法签名更改为:

    [WebMethod]
    LoginSpecial(string Username, string Password)
    {
    }
    

    您提供给 AJAX 调用的 data 属性的 JSON 对象的属性名称必须与您的 Web 服务方法签名的参数名称相匹配。

    如果要提交参数列表,则必须提交对象数组并在 Web 服务中创建包装器 .NET 对象,如下所述:

    Using jQuery to POST Form Data to an ASP.NET ASMX AJAX Web Service

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      在服务器端使用库:

      Newtonsoft.Json.Linq => 从 http://james.networking.com/projects/json-net.aspx 下载 JSON.net

      还有代码:

              System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream);
      
              string line = "";
      
              line = sr.ReadToEnd();
      
              JObject jo = JObject.Parse(line);
      
              string something = (string)jo["something"]
      

      【讨论】:

        【解决方案3】:

        将参数添加到您的方法中,

        [WebMethod]
        LoginSpecial(string something)
        {
        }
        

        【讨论】:

        • 我试过了:public string LoginSpecial(string parameter) 现在当我调用 web 服务时,我收到以下错误:无效的 web 服务调用,缺少参数值:\u0027parameter\u0027.", "StackTrace “:”....
        • 在 jquery 数据中声明这样的参数:{ info = value, something = something},
        • ì 必须在星期一查看确切的语法
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多