jquery Implementation page Local refresh Ajax approach

Source: Internet
Author: User
Tags getscript php tutorial

This method is more go, common have the following several;
The full page Refresh method is described below: sometimes you might use
Window.location.reload () refreshes the current page.
Parent.location.reload () Refreshes the Father object (for frames)
Opener.location.reload () Refreshes the parent window object (for a single open window)
Top.location.reload () Refreshes the topmost object (for multiple windows)

$.get method, $.post method, $.getjson method, $.ajax method is as follows

The first two ways are basically the same
Copy code code as follows:

$.get ("default.php Tutorial", {ID: "1″, Page:" 2″},
function (data) {
Here is the callback method. Returns data. Here's how to deal with it.
});
Jquery.post (URL, [data], [callback], [Type]): Asynchronous request using POST method


Parameters:

URL (String): The URL address where the request is sent.

Data (MAP): (optional) The information to be sent to the server, expressed as a Key/value key-value pair.

Callback (function): (optional) the callback function (which is invoked only if the response return state is success) when loading succeeds.

Type (String): (optional) The official description is: type of data to is sent. The type that should actually be requested for the client (Json,xml, etc.)


This is a simple POST request function to replace the complex $.ajax. Callback functions can be invoked when the request succeeds. If you need to perform a function when an error occurs, use $.ajax. Sample code:

ajax.asp Tutorial x:

Response.ContentType = "Application/json";
Response.Write ("{result: '" + request["Name"] + ", Hello! (This message comes from the server) '} ');
JQuery Code:
$.post ("Ajax.aspx", {Action: "Post", Name: "Lulu"},
function (data, textstatus) {
Data can be xmldoc, jsonobj, HTML, text, and so on.
This This AJAX request option configuration information, please refer to Jquery.get ().
alert (Data.result);
}, "JSON");


$.getscript Method:
Copy code code as follows:

$.getscript ("Http://jqueryajax.com/jquery.js",
function () {
$ ("#go"). Click (function () {//callback method
$ (". Block"). Animate ({backgroundcolor: ' Pink '}, 1000)
. Animate ({backgroundcolor: ' Blue '}, 1000);
});
});

$.getjson just returned a different type of data
Copy code code as follows:

$.getjson ("default.php", {ID: "1″, Page:" 2″},
function (data) {
Note that the JSON data reference method returned here is "Data.info", and it is not clear that you can check out the JSON tutorial. There's not much to explain here.
});

$.ajax This method is estimated to use a lot of people. But I don't like to use it very much. Personally think the front two more convenient
Copy code code as follows:

$.ajax ({
Type: "POST",//Submitted types
URL: "some.php",//Submit Address
Data: "Name=john&location=boston",//Parameter
Success:function (msg) {//callback method
Alert ("Data Saved:" + msg);//This is the content of the method, like the Get method above
}
});


jquery is really a nice lightweight JS framework that can help us quickly develop JS applications and, to some extent, change our habit of writing JavaScript code.

Cut the crap, go straight to the point, let's take a look at some simple methods that encapsulate the Jquery.ajax () to make it easier for us to use, and of course, if you want to deal with complex logic, you need to use Jquery.ajax ().

1. Load (URL, [data], [callback]): Loads the remote HTML file code and inserts it into the DOM.

URL (String): The URL address of the requested HTML page.

Data (MAP): (optional parameters) sent to the server's key/value.

Callback (callback): (optional parameter) a callback function that does not need to be success when the request completes.

This method is passed by default using Get method, and is automatically converted to post if the [data] parameter is passed in. In JQuery 1.2, you can specify a selector to filter the loaded HTML document, and only the filtered HTML code will be inserted into the DOM. Syntax is like "URL #some > selector".

This method makes it easy to dynamically load some HTML files, such as forms.

Sample code:

$ (". Ajax.load"). Load ("http://www.111cn.net. Post",
function (ResponseText, textstatus, XMLHttpRequest) {
this;//here. This point is the current DOM object, that is $ (". Ajax.load") [0]
alert (responsetext);//Request returned content
alert (textstatus);//Request Status: Success,error
alert (XMLHttpRequest);//xmlhttprequest Object
});

Related Article

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.