如果不是使用form表单submit的形式,我们可以手动通过formdata传值(针对文件上传等)
好比:
<html> <head> <meta content="width=device-width" /> <link href="http://www.mamicode.com/~/jquery-easyui-master/themes/default/easyui.css" /> <link href="http://www.mamicode.com/~/jquery-easyui-master/themes/icon.css" /> <script src="http://www.mamicode.com/~/Scripts/jquery-1.10.2.js"></script> <script src="http://www.mamicode.com/~/jquery-easyui-master/jquery.easyui.min.js"></script> <title>Test</title> </head> <body> <input/> @*<input type="file"/>*@ <button>提交</button> <script> function submitV() { var dom = $(‘input[name="my"]‘)[0];//获取上传文件dom东西 var forms = $(‘<form></form>‘).append($(dom).clone());//复制dom东西,创建表单 var formData = new FormData(forms[0]); /* 由于easyui生成的dom有各类嵌套,直接通过id获取的dom不是一个type为file的input 这里可以通过设置name属性,通过name获取 formdata东西可以附加各类表单值,文件东西也是可以的 formData.append("myValue", ‘myValue‘); formData.append("my", $(‘input[name="my"]‘)[0].files[0]); 还有一种方法就是通过form表单的dom去实例化foamdata,好比: var formData = new FormData($(‘<form><input value="myvalue"/></form>‘)[0]); */ $.ajax({ url: ‘@Url.Action("TestPost")‘, type: ‘POST‘, data: formData, async: false, cache: false, contentType: false,// 报告jQuery不要去设置Content-Type请求头 processData: false// 报告jQuery不要去措置惩罚惩罚发送的数据 }); } </script> </body> </html>
此中,ajax参数说明:
参数名 类型 描述url String (默认: 当前页地点) 发送请求的地点。
type String (默认: "GET") 请求方法 ("POST" 或 "GET"), 默认为 "GET"。注意:其它 HTTP 请求要领,如 PUT 和 DELETE 也可以使用,但仅部分浏览器撑持。
timeout Number 设置请求超不时间(毫秒)。此设置将笼罩全局设置。
async Boolean (默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操纵必需期待请求完成才可以执行。
beforeSend Function 发送请求前可改削 XMLHttpRequest 东西的函数,如添加自界说 HTTP 头。XMLHttpRequest 东西是独一的参数。
function (XMLHttpRequest) {
this; // the options for this ajax request }
cache Boolean (默认: true) jQuery 1.2 新成果,设置为 false 将不会从浏览器缓存中加载请求信息。
complete Function 请求完成后回调函数 (请求告成或掉败时均挪用)。参数: XMLHttpRequest 东西,告成信息字符串。
function (XMLHttpRequest, textStatus) {
this; // the options for this ajax request }
contentType String
(默认: "application/x-www-form-urlencoded") 发送信息至处事器时内容编码类型。默认值适合大大都应用场合。报告处事器从浏览器提交过来的数据格局。
例如:我们提交数据时假如使用了 JSON2.js 中要领 JSON.stringify(obj) 格局化为json字符串后,再默认提交就会报错。这个时候就需要指定提交的内容格局为:"application/json"。
data Object,
String
发送随处事器的数据。
若data数据类型为JavaScript东西或数 组,Jquery在提交之前自动挪用JQuery.param()要领把要发送的数据编码成为"application/x-www-form- urlencoded"格局的数据(即 name=value&name1=value1);JavaScript东西必需为 Key/Value 格局;如果为数组,jQuery 将自动为差别值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 ‘&foo=bar1&foo=bar2‘;
若data数据类型为String类型,则直接默认该数据已经凭据"application/x-www-form-urlencoded"格局编码完成,不再转换。
processData选项可以控制是否进行转换。该选项默认为true。
dataType String