ajax寫header的參數

1.
 $.ajax({
      headers: {
        "token":userToken//此處放置請求到的用戶token
      },
      type: "POST",
      url: "XXXXX",//請求url
      contentType: "application/x-www-form-urlencoded",
      success: (data) => {
        if (data.return_code === "SUCCESS") {
          this.deviceList = data.return_info;
          // console.log(this.deviceList);
        } else {
          alert("請求錯誤!錯誤信息:" + data.return_msg);
        }
      },
      error: (err) => {
        this.loginLoading = false;
        alert("錯誤信息:" + JSON.stringify(err))
      }
    })
    
    在header裏面添加參數
    2.放在beforeSend的函數裏面,進行數據的傳輸,使用XMLHttpRequest對象
    $.ajax({
         type: "post",
         url:"http://127.0.0.1:4564/bsky-app/template/testPost",
         contentType: "application/json;charset=utf-8",
         data :JSON.stringify({"bodyName":"sdfsdf","date":"2017-11-28 07:34:01","price": 10.5,"tbId": 1}),
         dataType: "json",
         beforeSend: function (XMLHttpRequest) {
     		XMLHttpRequest.setRequestHeader("token", "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxOD.....");
         },
         success: function (data) {
        	 alert(data);
         },error:function(error){
             console.log(error);
         }
         })
複製代碼
相關文章
相關標籤/搜索