Jquery ajax, jqueryajax

Source: Internet
Author: User

Jquery ajax, jqueryajax
Asynchronous Refresh can be implemented in multiple ways. You can also use multiple JS frameworks. The following describes how to use AJAX implemented by the JQuery framework to verify whether the user name exists in jQuery. ajax.

 

HTTP requests load remote data.

 

Implemented through jQuery underlying AJAX. For easy-to-use high-level implementation, see $. get, $. post, and so on. $. Ajax () returns the created XMLHttpRequest object. In most cases, you do not need to directly operate on this object, but in special cases it can be used to manually terminate the request.

$. Ajax () has only one parameter: The parameter key/value object, which contains information about various configurations and callback functions. For detailed Parameter options, see.

Note: If you specify the ype option, make sure that the server returns the correct MIME information (for example, xml returns "text/xml "). Incorrect MIME types may cause unpredictable errors. See Specifying the Data Type for AJAX Requests.

NOTE: If dataType is set to "script", all POST requests will be converted to GET requests during remote requests (not in the same domain. (Because the script tag of DOM will be used for loading)

In jQuery 1.2, you can load JSON data across domains. You need to set the data type to JSONP during use. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function. When the data type is set to "jsonp", jQuery automatically calls the callback function.

Parameter List:

Parameter Name

Type

Description

Url

String

(Default: Current page address) the address of the request sent.

Type

String

(Default: "GET") Request Method ("POST" or "GET"). The default value is "GET ". Note: Other HTTP request methods, such as PUT and DELETE, can also be used, but are only supported by some browsers.

Timeout

Number

Set the request timeout (in milliseconds ). This setting overwrites the global setting.

Async

Boolean

(Default: true) by default, all requests are asynchronous requests. To send a synchronization request, set this option to false. Note: The synchronous request locks the browser. Other operations can be performed only after the request is completed.

BeforeSend

Function

Before sending a request, you can modify the function of the XMLHttpRequest object, for example, adding a custom HTTP header. The XMLHttpRequest object is a unique parameter.

Function (XMLHttpRequest ){

This; // the options for this ajax request

}

Cache

Boolean

(Default: true) New jQuery 1.2 function. setting this parameter to false will not load request information from the browser cache.

Complete

Function

Callback Function after the request is complete (called when the request is successful or fails ). Parameter: XMLHttpRequest object, success information string.

Function (XMLHttpRequest, textStatus ){

This; // the options for this ajax request

}

ContentType

String

(Default: "application/x-www-form-urlencoded") Content Encoding type when sending information to the server. The default value is applicable to most applications.

Data

Object,
String

The data sent to the server. Will be automatically converted to the request string format. The GET request will be appended to the URL. View the description of the processData option to disable automatic conversion. It must be in Key/Value format. If it is an array, jQuery automatically corresponds to the same name for different values. For example, {foo: ["bar1", "bar2"]} is converted to '& foo = bar1 & foo = bar2 '.

DataType

String

Expected data type returned by the server. If this parameter is not specified, jQuery will automatically return responseXML or responseText Based on the MIME information of the HTTP package and pass it as a callback function parameter. Available values:

"Xml": returns an XML document, which can be processed by jQuery.

"Html": returns plain text HTML information, including script elements.

"Script": returns plain text JavaScript code. Results are not automatically cached.

"Json": Return JSON data.

"Jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function.

Error

Function

(Default: automatically determines (xml or html) This method is called when a request fails. This method has three parameters: XMLHttpRequest object, error message, and (possibly) captured error object.

Function (XMLHttpRequest, textStatus, errorThrown ){

// Generally, textStatus and errorThown have only one value.

This; // the options for this ajax request

}

Global

Boolean

(Default: true) Whether to trigger a global AJAX event. Setting false does not trigger global AJAX events, such as ajaxStart or ajaxStop. Can be used to control different Ajax events

IfModified

Boolean

(Default: false) obtain new data only when the server data changes. Use the Last-Modified header information of the HTTP packet to determine.

ProcessData

Boolean

(Default: true) by default, data sent is converted to an object (technically not a string) with the default content type "application/x-www-form-urlencoded ". If you want to send the DOM tree information or other information that does not want to be converted, set it to false.

Success

Function

Callback Function after successful request. This method has two parameters: the server returns data and returns the status.

Function (data, textStatus ){

// Data cocould be xmlDoc, jsonObj, html, text, etc...

This; // the options for this ajax request

}

Here are several Ajax event parameters:BeforeSend,Success,Complete, error.We can define these events to process every Ajax request. Note that this in these Ajax events points to the option Information of the Ajax request (see the image of this in the get () method ).
Read the parameter list carefully. If you want to use jQuery for Ajax development, you must be familiar with these parameters.

Instance:

1. Request AJax. aspx

HTML code

<Div> <input id = "txtName" type = "text"/> <input type = "button" value = "check whether the user name exists" id = "btn" onclick =" judgeUserName (); "/> <div id =" showResult "style =" float: left "> </div>

JS Code

<Script type = "text/javascript" src = "CSS/jquery-1.3.2.js"> </script> <script type = "text/javascript"> function JudgeUserName () {$. ajax ({type: "GET", url: "AjaxUserInfoModify. aspx ", dataType:" html ", data:" userName = "+ $ (" # txtName "). val (), beforeSend: function (XMLHttpRequest) {$ ("# showResult "). text ("querying"); // Pause (this, 100000) ;}, success: function (msg) {$ ("# showResult" ).html (msg ); $ ("# showResult" ).css ("color", "red") ;}, complete: function (XMLHttpRequest, textStatus) {// hide the picture being queried}, error: function () {// error handling }}) ;}</script>

 

2. Page AjaxUserInfoModify. aspx

Background code

 

Protected void Page_Load (object sender, EventArgs e) {string userName = Request. queryString ["userName"]. toString (); if (userName = "James Hao") {Response. write ("the user name already exists! ");} Else {Response. Write (" You can use this user name! ");}}

 

3. Running Interface

(1) initialization Interface

(2) Querying the prompt page

 

(3) Verify that the user name already exists on the page

 

(4) The verification user name does not exist on the page

 

So far, AJAX has completed the function of verifying whether the user name exists.

The above article is intended to be useful for beginners of JS.

 

This article is reproduced to the author: Smart Life

Address: http://www.cnblogs.com/ywqu/archive/2009/06/03/1495680.html

 

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.