用Ajax调用web api,解决URL太长的问题;
本来是用的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太长的问题;的更多相关文章
- 开发程序过程中遇到的调用Web Api小问题
在用Umbraco Web Api开发程序时,前端使用React调用Web Api 当时是有一个页面Search.cshtml,把用React产生的脚本代码,在这个页面进行引用 写了一个Api, 调用 ...
- ASP.NET MVC4中调用WEB API的四个方法
http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...
- WebApi系列~通过HttpClient来调用Web Api接口
回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...
- 通过HttpClient来调用Web Api接口
回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...
- ajax调用.net API项目跨域问题解决
ajax调用.net API项目,经常提示跨域问题.添加如下节点代码解决:httpProtocol <system.webServer> <handlers> <remo ...
- jQuery跨域调用Web API
我曾经发表了一篇关于如何开发Web API的博客,链接地址:http://www.cnblogs.com/guwei4037/p/3603818.html.有朋友说开发是会开发了,但不知道怎么调用啊? ...
- Identity Server 4 从入门到落地(五)—— 使用Ajax访问Web Api
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...
- React Native教程 - 调用Web API
react-native官网Fetch介绍:https://facebook.github.io/react-native/docs/network.html#content react-native ...
随机推荐
- python @property 属性
在绑定属性时,如果我们直接把属性暴露出去,显然不合适,是通过getter和setter方法来实现的,还可以定义只读属性,只定义getter方法,不定义setter方法就是一个只读属性: class P ...
- POSIX多线程编程(上)-基本概念
线程概念 我们把正在计算机中执行的程序叫做"进程"(Process) ,而不将其称为程序(Program).所谓"线程"(Thread),是"进程&q ...
- 简单的map转换成Bean的工具
简单的map转换成Bean的工具 package com.sd.microMsg.util; import java.lang.reflect.Field; import java.lang.refl ...
- hdu 3718
一个二分图最大匹配的题: 匈牙利算法不熟: 建了个模,用最小费用最大流解决了 #include <iostream> #include <cstring> #define IN ...
- single page
http://msdn.microsoft.com/zh-cn/magazine/cc507641.aspx#S7 http://blog.nodejitsu.com/scaling-isomorph ...
- *[topcoder]LittleElephantAndIntervalsDiv1
http://community.topcoder.com/stat?c=problem_statement&pm=12822&rd=15707 第一次用C++提交,艰辛.首先想到可以 ...
- Delphi GDI+ Library
GDI+ LibraryThis library enables GDI+ functionality for Delphi 2009 and later. It differs from other ...
- Ember.js demo7
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1 ...
- linux PCI设备初始化过程
linux PCI设备初始化过程 start_kernel->rest_init 这个函数会启动一个核心线程0, 核心线程然后调用init -> do_basic_setup. 然后我们开 ...
- hdu4499Cannon(搜索)
链接 这样的叫迭代吗..最近多做些搜索题了要 分行分列搜 判断满足条件 #include <iostream> #include<cstdio> #include<cst ...