Is it possible to modify XMLHttpRequest data from beforeSend callback?

前端 未结 2 2024
走了就别回头了
走了就别回头了 2020-12-30 14:23

Is it possible to modify the data sent in an Ajax request by modifying the XMLHttpRequest object in the beforeSend callback? and if so how might I do that?

2条回答
  •  悲&欢浪女
    2020-12-30 14:52

    I was looking for this solution and wonder why I am not finding the s.data so I changed the request type to post and it was there, Looks like if you are using GET request the data property is not there, I guess you have to change the s.url

    for get method:

    $.ajax({
      type:'GET',
      beforeSend: function(xhr, s) {
        s.url += "&newProp=newValue";
      }
    });
    

    for post method:

    $.ajax({
      type:'POST',
      beforeSend: function(xhr, s) {
        s.data += "&newProp=newValue";
      }
    });
    

提交回复
热议问题