本文介绍了jQuery中angularjs拦截器的等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拦截从REST请求响应为$ httpProvider.interceptors确实在agularjs:
$ HTTP

我正在使用jQuery一个非常小的接口,并会不喜欢使用角只是为了这一点。
你有和想法?

其实我的解决真正的问题是一样的这一个:
Dealing以301和位置标头跨域

一个REST响应

不过,我想解决它与jQuery相同的方式。

我想这没有成功:(只抓0状态从来没有一个301 ......)

How事件处理程序之前,我可以拦截jQuery的Ajax响应?

要回答V31,我这样做:

  $。ajaxSetup({
    错误:功能(jqXHR,textStatus,errorThrown){
        如果(jqXHR.status == 301){
            警报(找不到元素。);
        }其他{
            的console.log(jqXHR.status);
            的console.log(错误:+ textStatus +:+ errorThrown);
        }
    }
});

下面是我的控制台:

它说:

  XMLHtt prequest无法加载*******。请求被重定向到**************,这是不允许的,需要preflight跨域请求。
0
错误:错误:


解决方案

您可以使用ajaxSetup的为了赶上301错误,这样的事情:

  $。ajaxSetup({
    错误:功能(jqXHR,textStatus,errorThrown){
        如果(jqXHR.status == 0){
            警报(找不到元素。);
        }其他{
            警报(错误:+ textStatus +:+ errorThrown);
        }
    }
});

更多关于

更新:

要处理,你需要启用跨域调用CO​​RS问题,是这样的:

  VAR应用= angular.module('对myApp',[]);  的app.config(函数($ httpProvider){
      //启用跨域调用
      $ httpProvider.defaults.useXDomain = TRUE;      //删除用于标识Ajax调用的头,将美元,工作p $ pvent CORS
      删除$ httpProvider.defaults.headers.common ['X-要求-随着'];
  });

正如你所提到,服务器端code是不是在你的手,你需要提高到当事人的请求,以在其API调用的响应这些头(如果尚未完成)

 访问控制允许来源'=> '*',
 访问控制允许的方法'=> [选项,GET,POST]

I would like to intercept response from REST request as $httpProvider.interceptors does in agularjs :https://docs.angularjs.org/api/ng/service/$http

I am making a very little interface with jQuery and would not like to use angular just for this.Do you have and Idea ?

Actually my real problem to fix is the same as this one :Dealing with a 301 and location headers for a REST response in cross-domain

But I would like to solve it the same way with jquery.

I tried this without success : (only catch a 0 status never a 301...)

How can I intercept ajax responses in jQuery before the event handler?

To answer V31, I did this :

$.ajaxSetup({
    error: function(jqXHR, textStatus, errorThrown) {
        if (jqXHR.status == 301) {
            alert("Element not found.");
        } else {
            console.log(jqXHR.status);
            console.log("Error: " + textStatus + ": " + errorThrown);
        }
    }
});

Here is my console :

Which says :

XMLHttpRequest cannot load *******. The request was redirected to '**************', which is disallowed for cross-origin requests that require preflight. 
0 
Error: error:  
解决方案

You can make use of ajaxSetup in order to catch 301 errors, something like this:

$.ajaxSetup({
    error : function(jqXHR, textStatus, errorThrown) {
        if (jqXHR.status == 0) {
            alert("Element not found.");
        } else {
            alert("Error: " + textStatus + ": " + errorThrown);
        }
    }
});

More on jquery ajax setup

UPDATE:

To handle CORS issue you need to enable cross domain calls, something like this:

var app = angular.module('myApp', []);

  app.config(function($httpProvider) {
      //Enable cross domain calls
      $httpProvider.defaults.useXDomain = true;

      //Remove the header used to identify ajax call  that would prevent CORS from working
      delete $httpProvider.defaults.headers.common['X-Requested-With'];
  });

As you have mentioned that the server side code is not in your hand you need to raise a request to that party in order to include these headers in the response of their API call (if not already done)

 'Access-Control-Allow-Origin' => '*', 
 'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST']  

这篇关于jQuery中angularjs拦截器的等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:33