jQuery、ajax提交fromdata数据

发布时间:2025-07-30 点击:14
需求说明:简单演示ajax提交fromdata类型数据。
代码说明:ajax在传输数据的时候基本格式大都是固定的,只需要修改传输类型即可。下面介绍基本的参数提交。
步骤一:建立html或者jsp页面,引入jquery-3.2.1.min.js(其他版本亦可)。
步骤二:建立文件选择输入框,上传按钮并给其id赋值。
步骤三:编写jquery、ajax代码,完成上传到指定controller。
下面是示例代码:
<!doctype html>
<html lang=en>
<head>
<script src=jquery-3.2.1.min.js></script>
<script>
$(function () {
$(#upload).click(function () {
$(#imgwait).show();
var formdata = new formdata();
formdata.append(myfile, document.getelementbyid(file1).files[0]);
formdata.append(type, image);
console.log(formdata);
$.ajax({
url: http://localhost:8080/xx/xx/uploadfile.do,
type: post,
data: formdata,
contenttype: false,
processdata: false,
success: function (data) {
console.log();
},
error: function () {
console.log(上传失败!);
}
});
});
});
</script>
</head>
<body>
选择文件:<input type=file id=file1 /><br />
<input type=button id=upload value=上传 />
</body>
</html>
总结:上面代码中ajax访问接口实现文件的上传功能,涉及到ajax跨越的问题这里就不做介绍了。

改善手机网站开发设计,提升利用率
营销网站建设如何确保品质
好用的云服务器具备什么特征
网站制作的构图规则_平顶山网站建设
SEO优化的一些小小的经验
网站原创内容是保持网站活力的血液
如何简单评估一个网站在搜索引擎中的权重
深圳网站建设要遵守的搭建原则有哪些?