Ajax request in jQuery----ajax request (2)

      The Ajax request in jQuery is actually an encapsulation of the native js request method at the bottom layer. So what does the Ajax request in jQuery look like?

     First the code:

$.ajax({
            url: 'xxx',  //请求地址
            type: 'get',  //请求方式 get或者post
            //请求参数
            data: {  //发送到服务器的数据(将自动转换为请求字符串格式)
                id: xx,
                username: 'xx'
            }, 
            dataType: 'json',  //用于设置响应体的类型
            beforeSend: function (xhr) {
                // 在所有发送请求的操作(open, send)之前执行
                console.log('beforeSend', xhr);
            },
            success: function (res) {
                // 只有请求成功(状态码为200)才会执行这个函数
                console.log(res);
            },
            error: function (xhr) {
                // 只有请求不正常(状态码不为200)才会执行
                console.log('error', xhr);
            },
            complete: function (xhr) {
                // 不管是成功还是失败都是完成,都会执行这个 complete 函数(一般成功与失败都需要执行的操作写在这里)
                console.log('complete', xhr);
            }
        });

There are some other parameters that can be added, so I won’t go into details here.

Guess you like

Origin blog.csdn.net/smallmww/article/details/132210475