【问题标题】:C# WebService send standard post request body, respond with JSONC# WebService 发送标准的 post 请求体,用 JSON 响应
【发布时间】:2012-03-29 11:50:19
【问题描述】:

我在为 .NET WebServices 制作动态 AJAX 表单提交方法时遇到了困难。

这个想法是发送一个从表单中的所有输入动态构建的请求。然后让服务器响应 JSON。

在提交表单时调用,它填充一个以索引为输入字段名称和值作为值的数组:

var params = [];

for( var i in inputs )
{
    if( inputs[i].type == 'text' || inputs[i].type == 'password' )
    {
        params[inputs[i].name] = inputs[i].value;
    }
}

发送 AJAX 请求时,我会运行一个循环来生成请求正文:

var l = 0;
for( var i in parameters )
{
    this.parameters += ( l > 0 ? '&' : '') + i + '=' + parameters[i];
    l++;
}

结果如下:

foo=bar&lol=haha

问题是脚本服务由于装饰器的原因只接受 JSON 作为请求体:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

因为我希望它返回 JSON 字符串而不是 XML。

有简单的解决办法吗?

【问题讨论】:

    标签: c# .net ajax json web-services


    【解决方案1】:

    我想出的解决方案是一个相当复杂的解决方法,它应该是简单的。基本上我做了一个接受 Xml 格式的函数。然后使用客户端轮询的更新方法将数据添加到格式化为 JSON 的更新模型。

    这是一个例子:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class JsonHTTPService : System.Web.Services.WebService
    {
        static JavaScriptSerializer JSON = new JavaScriptSerializer();
    
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public dynamic update()
        {
            if (Session["user"] == null)
            {
                Session.Add("user", new User());
            }
    
            User user = (User)Session["user"];
            user.responseModel = new ResponseModel();
    
            if (user.updateListeners.Count > 0)
            {
                foreach (var updateMethod in user.updateListeners)
                {
                    updateMethod.run();
                }
                return JSON.Serialize(user.responseModel);
            }
            return null;
        }
    
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        public void login(string email, string password)
        {
            if (Session["user"] == null)
            {
                return;
            }
    
            User user = (User)Session["user"];
            if (user.logged)
            {
                return;
            }
    
            if (user.Authenticate(email,password))
            {
                user.logged = true;
                user.updateListeners.Add(new LoginScreenRemover());
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

      我在我的应用程序中尝试了相同的方法,并且成功了。希望它也能解决您的问题。

      【讨论】:

        【解决方案3】:

        试试这个JSON.stringify(params);

        【讨论】:

          猜你喜欢
          • 2019-02-23
          • 2017-03-01
          • 2020-07-29
          • 2018-05-27
          • 1970-01-01
          • 2017-10-12
          • 2018-09-16
          • 2011-01-12
          • 1970-01-01
          相关资源
          最近更新 更多