Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Detect请求用于$httpProvider拦截器中的Html模板或rest数据_Angularjs - Fatal编程技术网

Angularjs Detect请求用于$httpProvider拦截器中的Html模板或rest数据

Angularjs Detect请求用于$httpProvider拦截器中的Html模板或rest数据,angularjs,Angularjs,我需要截获http请求,将基本url(域url)添加到http请求中,并添加用于身份验证的访问令牌(基于令牌),模板域与rest api域不同,但我的问题是我无法识别请求是针对模板还是rest api(数据)。我的代码是: .config(["$httpProvider", function($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaul

我需要截获http请求,将基本url(域url)添加到http请求中,并添加用于身份验证的访问令牌(基于令牌),模板域与rest api域不同,但我的问题是我无法识别请求是针对模板还是rest api(数据)。我的代码是:

.config(["$httpProvider", function($httpProvider) {

        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common["X-Requested-With"];

        $httpProvider.interceptors.push("httpMiddleware");
    }
]).factory("httpMiddleware", [
    function () {
        return {
            request: function (config) {
                // Need to recognise request is for html template or rest api
                var baseUrl = "http://localhost:9926/Api/";
                config.url = baseUrl  + config.url;
                return config;
            }
        };
    }
]);;

您只需在请求的URL中检查
.html
字符串即可

request: function (config) {
     // Check whether the requested url contains .html
     if(config.url.contains('.html')) 
     {
         // request for html templates
     }
     else{
        // request for REST api
        // you can also do check for string API in the request url to verify that the request is to our api
     }
     var baseUrl = "http://localhost:9926/Api/";
     config.url = baseUrl  + config.url;
     return config;
}

您只需在请求的URL中检查
.html
字符串即可

request: function (config) {
     // Check whether the requested url contains .html
     if(config.url.contains('.html')) 
     {
         // request for html templates
     }
     else{
        // request for REST api
        // you can also do check for string API in the request url to verify that the request is to our api
     }
     var baseUrl = "http://localhost:9926/Api/";
     config.url = baseUrl  + config.url;
     return config;
}

验证请求的方法有很多,比如检查请求是否有特定于API的头,检查URI是否有模板路径,或者Abhilash提到的检查URI扩展

以下是示例代码:

.config(["$httpProvider", function($httpProvider) {

        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common["X-Requested-With"];

        $httpProvider.interceptors.push("httpMiddleware");
    }
]).factory("httpMiddleware", [
    function () {
        return {
            request: function (config) {
                // Need to recognise request is for html template or rest api
                  if(config.headers['someAttr'] == valueToCheck){ 
                  // or if(config.url.contains('/path/template')){ 
                  // or if(config.url.contains('.html')){ 
                  var baseUrl = "http://localhost:9926/Api/";
                  config.url = baseUrl  + config.url;
                }
                return config;
            }
        };
    }
]);;

验证请求的方法有很多,比如检查请求是否有特定于API的头,检查URI是否有模板路径,或者Abhilash提到的检查URI扩展

以下是示例代码:

.config(["$httpProvider", function($httpProvider) {

        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common["X-Requested-With"];

        $httpProvider.interceptors.push("httpMiddleware");
    }
]).factory("httpMiddleware", [
    function () {
        return {
            request: function (config) {
                // Need to recognise request is for html template or rest api
                  if(config.headers['someAttr'] == valueToCheck){ 
                  // or if(config.url.contains('/path/template')){ 
                  // or if(config.url.contains('.html')){ 
                  var baseUrl = "http://localhost:9926/Api/";
                  config.url = baseUrl  + config.url;
                }
                return config;
            }
        };
    }
]);;