jQuery、ajax提交fromdata數(shù)據(jù)

2022-05-31    分類(lèi): 網(wǎng)站建設(shè)

需求說(shuō)明:簡(jiǎn)單演示ajax提交fromData類(lèi)型數(shù)據(jù)。

代碼說(shuō)明:ajax在傳輸數(shù)據(jù)的時(shí)候基本格式大都是固定的,只需要修改傳輸類(lèi)型即可。下面介紹基本的參數(shù)提交。

步驟一:建立html或者jsp頁(yè)面,引入jquery-3.2.1.min.js(其他版本亦可)。

步驟二:建立文件選擇輸入框,上傳按鈕并給其ID賦值。

步驟三:編寫(xiě)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>

總結(jié):上面代碼中ajax訪(fǎng)問(wèn)接口實(shí)現(xiàn)文件的上傳功能,涉及到ajax跨越的問(wèn)題這里就不做介紹了。

文章題目:jQuery、ajax提交fromdata數(shù)據(jù)
URL網(wǎng)址:http://www.muchs.cn/news12/161812.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站排名、企業(yè)網(wǎng)站制作、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、軟件開(kāi)發(fā)、定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)