Home > Web Front-end > JS Tutorial > jQuery and Ajax and serialization_jquery

jQuery and Ajax and serialization_jquery

WBOY
Release: 2016-05-16 15:16:58
Original
1261 people have browsed it

About AJAX

The so-called Ajax, the full name is Asynchronous JavaScript and XML. (That is, asynchronous JS and XML)

To put it simply, it means sending and getting data without refreshing the page, and then updating the page.

Advantages of Ajax

•No plug-in support required
•Excellent user experience
•Improve the performance of web applications
•Reduce the burden on servers and bandwidth

Disadvantages of Ajax

•Insufficient browser compatibility
• Breaks the normal functionality of the browser’s forward and back buttons
•Inadequate support for search engines
•Lack of development and debugging tools

Well, these were all shortcomings from a few years ago. Technology is developing rapidly, and these shortcomings will gradually be made up. At least it is not difficult to debug Ajax now.

The core of Ajax is the XMLHttpRequest object, which is the key to Ajax implementation.

I won’t mention the traditional examples of implementing Ajax. It’s so painful that I didn’t even remember it. I searched a lot on the Internet.

About Ajax in jQuery

$.ajax() method is the Ajax method that encapsulates the most original js.

load(), $.get(), $.post() are encapsulated $.ajax()

$.getScript() and $.getJSON() are further encapsulations.

•load() method •Use: Load remote HTML code and insert it into the DOM. It is usually used to obtain static data files. The structure is: load(url [,data] [,callback]). •url is the requested address
•data is optional and is the parameter object sent to the server
•callback is a callback function, which is called whether the request succeeds or fails
•You can even add filters to the address when loading the page

$("#resDiv").load("test.html .myClass");//这个div里只载入test.html页面里面 样式为myClass 的元素


//举一个完整的例子
$(function(){
$("#resDiv").load("text.php",{name:"troy",textInfo:"hello"},function(responseText,textStatus,XMLHttpRequest){
//responseText:请求返回的内容
//textStatus: 请求状态:success、error、notmodiffied、timeout 4种 
//XMLHttpRequest: XMLHttpRequest对象
});
}); 
Copy after login

•$.get() method •Obviously the calling method is different, so this function is a global function of jQuery. The previous methods and load() all operate on jQuery objects
•The $.get() method uses the GET method to make asynchronous requests. The structure is: $.get(url [,data] [,callback] [,type]) •The first three parameters will not be mentioned. The only difference is callback is only called if the request is successful
•The type parameter is the format of content returned by the server, including xml, html, script, json, text and _default
•Example

$("#send").click(function()
$.get("get1.php"
,{
username:$("#username").val(),
content:$("#content").val()
}
,function(data,textStatus){
//data: 返回的内容,可以是XML文档、JSON文件、HTML片段
//textStatus: 请求状态:success、error、notmodiffied、timeout 4种
}
)
}) 
Copy after login

•$.post() method •It plays the same way as the get method, but one is the get method and the other is the post method.
•$.getScript() method •Sometimes it is not necessary to obtain all scripts when the page is first loaded, so jQuery provides the getScript method to directly load js files.
•Example

$('#send').click(function(){
$.getScript('test.js',function(){
//do something 这个时候脚本已经加载了,不需要再对js文件进行处理
});
}); 
Copy after login

• $.getJSON() method • Used to load JSON files, the usage is the same as above, except that the json data is returned

$('#send').click(function(){
$.getJSON("myurl",function(data){
var html="";
$.each(data,function(commentIndex,comment){
html+=commentIndex+":"+comment['username']+";";
})
alert(html);
})
});
//注意一下ecch这种玩法,同样是个全局函数。他的回调函数中,第一个参数为成员的索引,第二个为变量和内容 
Copy after login

By the way, expand on JSONP for cross-domain access

$("#send").click(function(){
$.getJSON("http://www.某网站.com/services/getMyCmpJson?tags=car&tagmode=any&format=json&jsoncall back=?"
,function(data){
//某些操作
}
)
})
Copy after login

//JSONP is an unofficial protocol, which uses a combination of json and

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template