jquery 使用$.ajax 时获取原生XMLHttpRequest 对象
2014-01-10 18:18
1151 查看
<script> $('.screenshot_input').change(function (){ //获取file对象 即相当于可以直接post的$_FILES数据 var domFile = $(this)[0].files[0] var domForm = $('#testForm')[0] //将form对象直接作为参数 new FormData对象 var formData = new FormData(domForm); //追加file 对象 formData.append('file',domFile); $.ajax({ url:'{{ path( 'acme_admin_app_dealAppScreenShot') }}', type: "POST", data: formData, processData: false, // tell jQuery not to process the data contentType: false, // tell jQuery not to set contentType xhr: function(){ //这是关键 获取原生的xhr对象 做以前做的所有事情 var xhr = jQuery.ajaxSettings.xhr(); xhr.upload.onload = function (){ alert('finish downloading') } xhr.upload.onprogress = function (ev) { if(ev.lengthComputable) { var percent = 100 * ev.loaded/ev.total; console.log(percent,ev) } } return xhr; }, success:function(data){ alert(data) } }) }) </script>
Array ( [name] => cooff [email] => qq.comaa ) Array ( [file] => Array ( [name] => yunce2.0测试.apk [type] => application/vnd.android.package-archive [tmp_name] => /tmp/php4Jxt0c [error] => 0 [size] => 6476627 ) )
一次过把form表单 和 file 对象发送到PHP后端controller 处理起来非常方便
相关文章推荐
- jQuery 利用$.ajax 时获取原生XMLHttpRequest 对象的方法
- 习惯了使用jQuery的ajax方法,看看原生js使用xmlhttpRequest实现ajax请求
- 使用XMLHttpRequest对象完成原生的AJAX请求
- 原生Ajax的XMLHttpRequest对象使用指南
- 【技术】使用原生XHR(XMLHttpRequest)对象来获取并包括HTML片段
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- 使用JS和Ajax发出异步请求,XmlHttpRequest对象详解
- AJAX 简介 及 XMLHttpRequest对象的使用方法
- 【AJAX】XMLHttpRequest对象的使用
- 使用xmlHttprequest 发送异步请求(Ajax核心对象)
- struts2中Ajax的操作实现及其核心对象XMLHttpRequest对象的使用
- JQuery——Ajax之XMLHTTPRequest对象
- 关于JS中ajax的XMLHttpRequest对象的使用
- AJAX技术使用XMLHttpRequest对象传递参数的中文乱码问题
- ajax-xmlHttprequest对象的对象创建和属性使用
- AJAX的初步介绍以及XMLHttpRequest对象的五步使用法
- 不使用XMLHttpRequest对象实现Ajax效果
- AJAX使用 以及 AJAX核心:XMLHttpRequest对象
- Ajax -- 获取XMLHttpRequest对象
- Ajax核心对象 XMLHTTPRequest 五步学会使用