本地是用localhost访问项目的,页面里面请求都是写的相对路径,每个页面逐个修改太麻烦,可以用ajaxSetup对请求全局设置。可以加请求头,可以判断未登录跳转,非常实用。本例结合layui来展示如何使用,在config.js里添加:

    layui.$.ajaxSetup({
    
        beforeSend: function(jqXHR, settings) {
        
             if(location.hostname=='localhost'){

                  settings.url='https://www.abc.com/'+settings.url;

             }

            /*header里加请求头参数*/
           jqXHR.setRequestHeader('token', '123')
 
        },
        success:function(result,status,xhr){

        },
        error:function(xhr,status,error){

        },
        complete:function(xhr,status){

            var res=JSON.parse(xhr.responseText);
            if(res.status==401){//未登录
                 window.location.href='/'
            }
            
        }
      });