使用ajax请求的标头

时间:2012-10-04 10:23:29

标签: html5 jquery

如何使用Ajax请求设置标头?在下面的代码中

$.ajax({
    type: type, //GET or POST or PUT or DELETE verb
    url: requestURL, // Location of the service        
    //  contentType: "application/x-www-form-urlencoded", // content type sent to server

    dataType: "xml", //Expected data format from server
    processData: false, //True or False
    success: successCallback, //On Successfull service call
    error: serviceFailed// When Service call fails
});

1 个答案:

答案 0 :(得分:1)

可以使用beforeSend(jqXHR, settings)函数和setRequestHeader方法管理请求标头,例如

$.ajax({
  ...
  beforeSend  : function(xhr) { 
    xhr.setRequestHeader('content-Type', 'application/x-www-form-urlencoded');
  }
});