Home  >  Article  >  Web Front-end  >  How to use jquery ajax

How to use jquery ajax

(*-*)浩
(*-*)浩Original
2019-06-01 16:01:372949browse

ajax() method loads remote data through HTTP requests.

How to use jquery ajax

This method is the underlying AJAX implementation of jQuery. For simple and easy-to-use high-level implementations, see $.get, $.post, etc. $.ajax() returns the XMLHttpRequest object it created. In most cases you won't need to manipulate this function directly unless you need to manipulate less commonly used options for more flexibility.

In the simplest case, $.ajax() can be used directly without any parameters.

Note: All options can be set globally through the $.ajaxSetup() function.

Syntax

jQuery.ajax([settings])//  发请求并且能得知成功还是失败。

settings, optional. A collection of key-value pairs used to configure Ajax requests. The default value for any option can be set via $.ajaxSetup().

type: Type, "POST" or "GET", the default is "GET".

url: The address to send the request.

data: is an object, along with the data sent to the server with the request d

ataType: the data type expected to be returned by the server. If not specified, jQuery will automatically make intelligent judgments based on the MIME information contained in HTTP. Generally, we use the json number, which can be set to "json".

success: is a method, a callback function after the request is successful. Pass in the returned data and a string containing the success code.

error: is a method, this function is called when the request fails. Pass in the XMLHttpRequest object.

Example:

$(document).ready(function(){
    $("#searchBtn").click(function(){
        $.ajax({
            type:"GET",
            url:" https://api.passport.xxx.com/checkNickname?username="+mylogin.username+"&token="+mylogin.token+"&nickname="+nickname+"&format=jsonp&cb=?",
            dataType:"json",
            success:function(data){
                if(data.errorCode==0){
                                         $("#nickname").val(mylogin.nickname);                             
                                 }else{
                                         $("#nickname").val("用户");                         
                                 }
            },
            error:function(jqXHR){
                console.log("Error: "+jqXHR.status);
            }
        });
    });
});

The above is the detailed content of How to use jquery ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn