jQuery—自定义HTTP请求
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) { } // 请求完成后最终执行参数 });