码迷,mamicode.com
首页 > Windows程序 > 详细

用Ajax调用web api,解决URL太长的问题;

时间:2017-01-09 13:31:58      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:传输   upd   local   pen   ted   reg   style   form   element   

本来是用的WCF,但是服务需要多种方式调用(后台+前端Ajax),最终局面就是我在WCF每个服务中都判断一下↓

            #region 解决接收不到Ajax中传来的参数...
            if (jsonParames == null && HttpContext.Current.Request.QueryString["参数"] != null)
                jsonParames = HttpContext.Current.Request.QueryString["参数"];
            else if (jsonParames == null && HttpContext.Current.Request.QueryString["参数"] == null)
                return JSON.Instance.ToJSON("{\"ret\":\"0\",\"msg\":\"参数为空.\"}");
            #endregion

 但是Ajax中用QueryString传输数据的话,有长度限制。

找了下,还有人说可以用form来解决提交的

<form id=form0 method=POST action=http://localhost:22377/api/Article_/Article_Update/ onsubmit = "set_v()">
<input id=id_v type=hidden value=value_default name=jsonParames/>
<input type="submit" value="XXX" class=""  />
</form>

不过提交完了之后直接就会将页面跳转到‘action‘所指向的页面。。。

 

最后还是决定再发布一个web api,然后用Ajax来解决

    <form id="form1">
        <div>
            ID:<input type="text" id="id_ID" />
            <br />
            <input type="button" value="POST" id="getPersons" />
        </div>
        <div id="ret">

        </div>
    </form>
    <script type="text/javascript">
    
    
    function set_v(){
    alert(‘set value!‘);
    var v = ‘too long data .‘;
    document.getElementById("id_v").value = v;
    alert(document.getElementById("id_v").val());
    }

        $(‘#getPersons‘).click(function () {
            document.getElementById("ret").innerHTML = ‘‘;//清楚上次查询内容...
            var ID = $("#id_ID").val();
            $.ajax({
                type: ‘POST‘,
                url: ‘http://localhost:22377/api/Article_/Article_Update‘,//?jsonParames=‘ + jsonParames,
                //dataType: ‘JSONP‘,//如果这行不注释请求的‘type‘就是GET,(哪怕第一行就规定了type:‘POST‘)
                //contentType:"application/json; charset=utf-8;",//这行不注释说不允许跨域调用!!!
                data: {
                    "jsonParames": ‘{"id":"0"}‘//jsonParames和web api中的参数名对应;
                },
                success: function (_data) {
                    alert(_data);
                    var info = eval(‘(‘ + _data + ‘)‘);
                    //alert(data);
                    var list = eval(‘(‘ + info.list + ‘)‘);
                    var fragment = document.createDocumentFragment();
                    $.each(list, function (i, field_list) {
                        $.each(field_list, function (i, field) {
                            var item = document.createElement("li");
                            item.appendChild(document.createTextNode(‘[‘ + i + ‘]‘ + ‘...‘ + field));
                            fragment.appendChild(item);
                            //alert(i + "...is..." + field);
                        })
                    })
                    $("#ret").append(fragment);
                },
                error: function () { }
            });
        });

    </script>

 web api 中;

        [HttpPost, HttpGet]
        public string Article_Update([FromBody]string jsonParames)
        {

            if (string.IsNullOrEmpty(jsonParames) || jsonParames.ToLower() == "jsonparames")
            {

                jsonParames = System.Web.HttpContext.Current.Request.Form["jsonParames"];

            }
            return "xx";
}

 

用Ajax调用web api,解决URL太长的问题;

标签:传输   upd   local   pen   ted   reg   style   form   element   

原文地址:http://www.cnblogs.com/love-zf/p/6264766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!