ajax如何增加请求头

代码如下(主要关键就是headers,大家可以根据需要来增加请求头):

     $.ajax({
          type: "POST",
          timeout: 10000, // 超时时间 10 秒
          headers: {
           'token': "U2PGBjguSuJg",
           'version': "1.0",
            'from': "test",
          },
      url: "http://www.yctech.com/api/login",
      data:{"username":username,"password":password},
          dataType: "json",
          success: function(data) {

        if(data.code=="111111"){
            layui.use('layer', function(){
                          var layer = layui.layer;

                          layer.msg(data.msg,{icon:1});
             });

        }else if(data.code=="000000"){

            layui.use('layer', function(){
                          var layer = layui.layer;

                          layer.msg(data.msg,{icon:1});
                    });
                    setTimeout(() => {

                        closeLayui();
            }, 700);

        $("#login").hide();

        $("#exit").show();

        $("#user").html(data.data.nickName);

        }

          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
                    console.log(XMLHttpRequest.readyState);
                    console.log(textStatus);
          },
          complete: function(XMLHttpRequest, status) { //请求完成后最终执行参数 
          }
      })
       }

 

posted @ 2019-02-17 13:25  挑战者V  阅读(24606)  评论(0编辑  收藏  举报