


Ajax upload method to implement js processing based on the data returned by the server
This article mainly introduces the method of Ajax upload to implement js processing based on the data returned by the server. The example analyzes the related skills of Ajax request and java processing and returning the server-side data request
The example of this article describes Ajax upload implements js processing method based on the data returned by the server. Share it with everyone for your reference. The details are as follows:
It is said clearly in Ajax that it is better to use form submission, add an iframe to the current page, and jump the submitted content to the iframe, creating an illusion that the page is not refreshed.
I have done uploading before, basically using the commons-fileupload component. The basic step is to use the servlet to process the upload, and then use the PrintWrite object instance to output the display content. It can be to directly output the content, or it can be Output scripts for operations such as
response.getWriter().write("<script type=\"text/javascript\"> parent.item_update.uploadUponSize();</script>");
or
response.getWriter().write("上传成功!");
. This approach is to encapsulate all page-side operations into servlets. Now one requirement is that you cannot access the server-side servlet, but the upload is successful. After that, the server will only return an identifier, and then operate on the page.
You can trigger a load event when the form is submitted to this iframe, so the idea for this requirement is:
1. When the form is submitted, register the load event for the iframe.
2. Then use js to judge the returned flag bit.
3. Remove binding events to avoid multiple binding events.
Post an example below.
It is simpler for the server side, only one flag 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 { 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 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 type="button" value="Submit" onclick="submitForm()"/> </form> <iframe name="hidden_iframe" id="hidden_iframe" width="300" height="200"> </iframe>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to solve the 403 error when django uses ajax post data
A brief analysis of IE's Ajax request results Caching issues
Comparative explanation of the use of various AJAX methods
The above is the detailed content of Ajax upload method to implement js processing based on the data returned by the server. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

What should I do if I can’t enter the game when the epic server is offline? This problem must have been encountered by many friends. When this prompt appears, the genuine game cannot be started. This problem is usually caused by interference from the network and security software. So how should it be solved? The editor of this issue will explain I would like to share the solution with you, I hope today’s software tutorial can help you solve the problem. What to do if the epic server cannot enter the game when it is offline: 1. It may be interfered by security software. Close the game platform and security software and then restart. 2. The second is that the network fluctuates too much. Try restarting the router to see if it works. If the conditions are OK, you can try to use the 5g mobile network to operate. 3. Then there may be more

How to install PHPFFmpeg extension on server? Installing the PHPFFmpeg extension on the server can help us process audio and video files in PHP projects and implement functions such as encoding, decoding, editing, and processing of audio and video files. This article will introduce how to install the PHPFFmpeg extension on the server, as well as specific code examples. First, we need to ensure that PHP and FFmpeg are installed on the server. If FFmpeg is not installed, you can follow the steps below to install FFmpe

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make
