Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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
Javascript 使用angularjs处理ajax调用_Javascript_Jquery_Angularjs_Ajax - Fatal编程技术网

Javascript 使用angularjs处理ajax调用

Javascript 使用angularjs处理ajax调用,javascript,jquery,angularjs,ajax,Javascript,Jquery,Angularjs,Ajax,我想在调用http请求(Ajax GET和POST)之前执行其他操作 假设我有以下代码: function CallHTTP() { $.ajax({ url: '/abcd/abcd', type: 'POST', success: function () { alert("Success"); }, error: function (arg1, arg2, arg3) {

我想在调用http请求(Ajax GET和POST)之前执行其他操作

假设我有以下代码:

function CallHTTP()
{

    $.ajax({
        url: '/abcd/abcd',
        type: 'POST',
        success: function () {
            alert("Success");
        },
        error: function (arg1, arg2, arg3) {
            alert("error");
        }

    });

}

$(document).ready(function () {
    CallHTTP();
});
我想使用angularjs拦截器处理上述请求。可能吗

我尝试了以下几点:

 angular.module('app', [])
          .config(['$httpProvider', function ($httpProvider) {
              $httpProvider.interceptors.push(['$location', '$q', function ($location, $q) {
                  return {
                      'request': function (request) {
                          console.log("abcd");
                          return request;
                      },
                      'responseError': function (response) {
                          if (response.status === 401) {
                              console.log("abcd2");
                              // do stuff
                          }
                          console.log("abcd1");
                          // otherwise, default behaviour
                          return $q.reject(response);
                      }
                  };
              }]);
          }]);
但当ajax调用发生时,上面的代码不会执行。如何实现


谢谢。

您正在使用jquery ajax调用,并且您的设置在angular interceptor中。使用$http使用角度设置。

您正在使用jquery ajax调用,并且您的设置位于角度拦截器中。使用$http使用角度设置。

拦截器截获请求/响应交换,但不执行。您必须使用
$http
。如果要使用jquery执行此操作,请使用jquery ajax事件:$(document.bind(“ajaxSend”,function(){console.log('before request');});拦截器拦截请求/响应交换,但不执行它。您必须使用
$http
。如果要使用jquery执行此操作,请使用jquery ajax事件:$(document.bind(“ajaxSend”,function(){console.log('before request');});