Ajax upload implementation based on server-side return data for JS processing method _ajax Related

Source: Internet
Author: User

This article describes the Ajax upload implementation based on server-side return data for JS processing method. Share to everyone for your reference. Specifically as follows:

Ajax upload plainly or use form form submission, in the current page add an IFRAME, will submit content jump to the IFRAME, resulting in a page without the illusion of refreshing.

Previously did upload, basically is the use of commons-fileupload components, the basic step is to use the servlet processing after uploading, using Printwrite object instance output display content, can be directly output content, can be output script to operate as

Copy Code code as follows:
Response.getwriter (). Write ("<script type=\ text/javascript\" > parent.item_update.uploadUponSize (); </ Script> ");

Or

Copy Code code as follows:
Response.getwriter () write ("Upload success!"). ");

This approach is to encapsulate the operations on the page side into the servlet, and now one requirement is that you do not have access to the server-side servlet, and after the upload succeeds, the server returns only one identifier and then operates on the page.
A load event can be triggered when submitting to this IFRAME according to form forms, so the idea for this requirement is:

1. Register the Load event for the IFRAME when submitting form forms.

2, and then use JS to return the logo bit to judge the operation.

3. Remove binding events to avoid multiple binding events.

An example is attached below.

For a simple server-side, only one flag bit will be returned.

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 C10/>this.dopost (request, response); 
  protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException C13/>printwriter writer = Response.getwriter (); 
    Response.setcontenttype ("text/html"); 
    Writer.print ("<root>ok</root>"); 
  } 
 

JS file

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 file

<form action= "uploadservlet.do" id= "Form2" enctype= "Multipart/form-data" method= "post" target= "Hidden_iframe" >
  <input type= "hidden" name= "method" Value= "Uploadexcel"/><input "button" type= "Submit" Onclick= "SubmitForm ()"/>
</form>
<iframe name= "Hidden_iframe" id= "Hidden_iframe" width= "300" height= "> </iframe>"


This allows the page to be manipulated based on the content returned by the page.

I hope this article will help you with your AJAX programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.