[关闭]
@KingMe 2016-01-05T22:10:21.000000Z 字数 1462 阅读 1201

JS 片段收集

JS 收集


根据IE版本,选择不同的JS

<!--[if lt IE 9]>  
    <script src="jquery-1.9.0.js"></script>  
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->  
<!--[if gte IE 9]><!-->  
    <script src="jquery-2.0.0.js"><</script>  
<!--<![endif]-->  

POST请求 request payload 转 form data

02 Jan 2016
后端方面用了token验证,每个请求的header里都要求加上token

前端项目里主要用到了Angular, 少部分用了jQuery

以下是两个方案的解决办法

jQuery
function initJqueryAjaxSetting() {
    jQuery.ajaxSetup( {  
        type: "POST", 
        "contentType": "application/x-www-form-urlencoded",
        headers: {
            'userToken': tools.loginManager.getToken()
        },  
        success: function( data, textStatus, jqXHR ){
            tools.loginManager.checkResponse(data);
        }
    });
}
Angular
app.factory('UserInterceptor', ["$q","$rootScope",function ($q,$rootScope) {
    return {
        request:function(config){
            config.headers["userToken"] = tools.loginManager.getToken();  
            return config;
        },
        response: function(response) {
            var deferred = $q.defer();  
            tools.loginManager.checkResponse(response.data);  
            deferred.resolve(response);  
            return deferred.promise; 
        }
    };
}]);  

app.config(function ($stateProvider, $httpProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        $httpProvider.defaults.transformRequest = function(data) {
        var str = [];
        for (var p in data)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(data[p]));
        return str.join("&");
    };
    $httpProvider.defaults.headers.post['userToken'] = tools.loginManager.getToken();     $httpProvider.interceptors.push('UserInterceptor'); 
}); 
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注