溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ajax上傳怎么實現(xiàn)根據(jù)服務(wù)器端返回數(shù)據(jù)進行js處理

發(fā)布時間:2021-08-04 09:58:34 來源:億速云 閱讀:98 作者:chen 欄目:web開發(fā)

這篇文章主要介紹“Ajax上傳怎么實現(xiàn)根據(jù)服務(wù)器端返回數(shù)據(jù)進行js處理”,在日常操作中,相信很多人在Ajax上傳怎么實現(xiàn)根據(jù)服務(wù)器端返回數(shù)據(jù)進行js處理問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Ajax上傳怎么實現(xiàn)根據(jù)服務(wù)器端返回數(shù)據(jù)進行js處理”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

本文實例講述了Ajax上傳實現(xiàn)根據(jù)服務(wù)器端返回數(shù)據(jù)進行js處理的方法。分享給大家供大家參考。具體如下:

Ajax上傳說白了還是使用form表單提交,在當前頁面加一個iframe,將提交的內(nèi)容跳轉(zhuǎn)到iframe中,造成一種頁面無刷新的錯覺。

以前也做過上傳,基本是是使用commons-fileupload組件,基本的步驟是使用servlet處理完上傳之后,使用PrintWrite的對象實例輸出顯示內(nèi)容,可以是直接輸出內(nèi)容,也可以是輸出script進行操作如

復制代碼 代碼如下:

response.getWriter().write("<script type=\"text/javascript\"> parent.item_update.uploadUponSize();</script>");


復制代碼 代碼如下:

response.getWriter().write("上傳成功!");

這種做法是把對頁面端的操作都封裝到servlet中,現(xiàn)在一個需求是你接觸不到服務(wù)器端servlet,而上傳成功之后服務(wù)器只會返回一個標志符,然后在頁面進行操作。
可以根據(jù)form表單提交到這個iframe時會觸發(fā)一個load事件,所以對于這個需求的思路是:

1、在form表單提交時,給iframe注冊load事件。

2、然后使用js對返回的標志位進行判斷操作。

3、移除綁定事件,避免多次綁定事件。

下面貼一個例子。

對于服務(wù)器端簡單一點,只會返回一個標志位。

package com.justsy.servlet; 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class UploadServlet extends HttpServlet { 
  private static final long serialVersionUID = 1L; 
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    this.doPost(request, response) ; 
  } 
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    PrintWriter writer = response.getWriter() ; 
    response.setContentType("text/html") ; 
    writer.print("<root>ok</root>") ; 
  } 
}

js文件

function submitForm(){ 
  $("#hidden_iframe").load(function(){ 
    var content = document.getElementById("hidden_iframe").contentWindow.document.body.innerHTML; 
    content = createXml(content); 
    var root = $(content).find("root").eq(0); 
    alert(root.text()); 
    $("#hidden_iframe").unbind("load"); 
  }); 
  document.getElementById("form2").submit(); 
} 
function createXml(str){ 
  if (document.all) { 
    var xmlDom = new ActiveXObject("Microsoft.XMLDOM"); 
    xmlDom.loadXML(str); 
    return xmlDom; 
  } 
  else { 
    return new DOMParser().parseFromString(str, "text/xml"); 
  } 
}

html文件

<form action="uploadServlet.do" id="form2" enctype="multipart/form-data" method="post" target="hidden_iframe">
  <input type="hidden" name="method" value="uploadExcel" /><input type="button" value="Submit" onclick="submitForm()"/>
</form>
<iframe name="hidden_iframe" id="hidden_iframe" width="300" height="200">
</iframe>

這樣就可以根據(jù)頁面返回的內(nèi)容對頁面進行操作了。

到此,關(guān)于“Ajax上傳怎么實現(xiàn)根據(jù)服務(wù)器端返回數(shù)據(jù)進行js處理”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI