当前位置: 移动技术网 > IT编程>开发语言>Jquery > jQuery—自定义HTTP请求

jQuery—自定义HTTP请求

2019年10月17日  | 移动技术网IT编程  | 我要评论

ajax设置自定义请求头的两种方法

$.ajax({
    url: 'http://www.baidu.com',
    type: 'get',
    data: json.stringify({"name":"love python!"}),
    // 方法一:设置headers请求头参数
    headers: {"content-type": "application/x-www-form-urlencoded", "accept": "text/plain"},
    success: function (data) {
        console.log(data)
    },
    error: function (xmlhttprequest, textstatus, errorthrown) {
        console.log(xmlhttprequest.status);
        console.log(xmlhttprequest.readystate);
    },
    complete: function(xmlhttprequest, status) { } // 请求完成后最终执行参数
});
$.ajax({
    url: 'http://www.baidu.com',
    type: 'get',
    data: json.stringify({"name":"love python!"}),
    contenttype: "application/x-www-form-urlencoded"
    // 方法二:设置headers请求头参数
    beforesend: function(request) {
        request.setrequestheader("accept", "text/plain");
        request.setrequestheader("content-type", "application/x-www-form-urlencoded");
    },
    success: function (data) {
        console.log(data)
    },
    error: function (xmlhttprequest, textstatus, errorthrown) {
        console.log(xmlhttprequest.status);
        console.log(xmlhttprequest.readystate);
        console.log(textstatus);
    },
    complete: function(xmlhttprequest, status) { } // 请求完成后最终执行参数
});

  

  

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网