This article mainly introduces the method of AJAX submission of form data. The example analyzes the principle and implementation skills of Ajax calling. Friends in need can refer to the following.
The example of this article describes the method of AJAX submission of form data. Share it with everyone for your reference. The details are as follows:
var TINY={}; TINY.ajax = function() { return { /** * @param string type 请求类型,post,get(目前只实现了这两种) * @param strng url 请求的地址 * @param object data 当使用post请求时的请求参数,ex: data=> {name:'adam'} * @param function callback 成功返回时的回调函数 */ call : function(type, url, data, callback) { var xhr = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');// for ie xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { callback.call(this, xhr.responseText); } } switch (type.toUpperCase()) { case 'POST': xhr.open('POST', url, true); // 这句比较重要 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); var formData = ''; for ( var i in data) { formData += i + '=' + data[i] + '&'; } xhr.send(formData); break; default: xhr.open('GET', url, true); xhr.send(null) break; } } } }();
Traverse each element of the form and organize the parameter values into JSON format
Special processing is done here for the CheckBox check box, and the value received in the background is for all check boxes The value is spliced with commas
function serialForm(form){ var e = form.elements; var ht = new Array(); var checkbox = new Array(); for(var i = 0; i < e.length; i++) { if(e[i].type=="checkbox"){ if(e[i].checked){ if(checkbox[e[i].name] != null) checkbox[e[i].name].push(e[i].value); else checkbox[e[i].name] = [e[i].value]; } } else { ht.push(e[i].name+":'"+e[i].value+"'"); ht.push(","); } } for (var ddd in checkbox ){ ht.push(ddd + ":'" + checkbox[ddd] + "'"); ht.push(","); } ht.push("nt:0"); return eval('({' + ht.join("") + '})'); };
AJAX call:
TINY.ajax.call('post', 'listfrom.do', serialForm(frm), function(data){ var ret = eval('('+data+')'); if(ret.errid==0){ alert(ret.text); window.location.reload(); } else{ alert(ret.text); } });
When it comes to the JSON format data returned by the server, the following formats are supported
String str = "[{\"mailAddr\":\"edison@163.com\"}, {\"mailAddr\":\"jay@263.com\"}]"; response.setContentType("application/json;charset=UTF-8"); response.getWriter().write(str);
Front-end call
function show(){ $.post("listmail.do", {"name" : "John"}, function(data){ for(var i = 0; i < data.length; i++){ alert(data[i].mailAddr); } }, "json"); }
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
A brief analysis of the use of Ajax in Asp.net MVC
##JQuery Ajax dynamically generates Table tables
AJAX cross-domain request JSONP to obtain JSON data
The above is the detailed content of AJAX submission form data example analysis. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
