AJAX重定向的困境,如何让重定向URL或如何设置重定向请求属性 [英] AJAX redirect dilemma, how to get redirect URL OR how to set properties for redirect request

查看:329
本文介绍了AJAX重定向的困境,如何让重定向URL或如何设置重定向请求属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的工作在谷歌浏览器,有没有什么帮助。这里是行为:

First, I'm working in Google Chrome, if that helps. Here is the behavior:

我把jQuery通过一个XHR请求到远程站点(这是一个Chrome扩展,我已经将所有的跨站点设置...):

I send an xhr request via jQuery to a remote site (this is a chrome Extension, and I've set all of the cross-site settings...):

$.ajax({
    type: "POST",
    contentType : "text/xml",
    url: some_url,
    data: some_xml,
    username: user,
    password: pass,
    success: function(data,status,xhr){
        alert(data);
    },
    error: function(xhr, status, error){
        alert(xhr.status);
    }
});

这是正在设置的URL返回一个302(这是预料),和Chrome遵循重定向(也预期)。

The URL that is being set returns a 302 (this is expected), and Chrome follows the redirect (also expected).

新的URL返回凭据,这是不是从原来的请求拉到一个提示,让Chrome浏览器显示登录对话框。如果我把原始凭据,我得到一个关于发送(这是一个有效的HTTP请求 - 200 - 远程服务器只是不喜欢的标题之一)无效请求响应

The new URL returns a prompt for credentials, which are not being pulled from the original request, so Chrome shows a login dialog. If I put in the original credentials, I get back a response about invalid request sent (it's a valid HTTP request -- 200 -- the remote server just doesn't like one of the headers).

当观看在Chrome开发窗口,还有送两个请求。首先是与AJAX请求设置的所有设置原始URL。二是要重定向URL,以GET,没有从POST字段,方法和没有凭据。

When viewing the developer window in Chrome, there are two requests sent. The first is to the original URL with all settings set in the AJAX request. The second is to the redirect URL, with a method of "GET", nothing from the "POST" field, and no credentials.

我很茫然,什么我能做的。我要么需要:

I am at a loss as to what I can do. I either need to:

  1. 获取重定向URL,这样我就可以发送第二个请求( xhr.getResponseHeader(位置)不工作),

有新的重定向请求preserve从原来要求的设置,或

Have the new redirect request preserve the settings from the original request, or

获取该错误来自这样我就可以发送另一个请求的最终URL。

Get the final URL that the error came from so I can send another request.

在理想情况下,我不希望用户在他们的凭据,把第二次在此对话框中,但我会采取什么我可以得到,如果我可以得到最终的URL。

Ideally I don't want the user to have to put in their credentials a second time in this dialog box, but I'll take what I can get if I can just get the final URL.

推荐答案

你没有与您的请求相关的认证会话?你填充一些头,以做身份验证或您使用的类似于HTTP基本身份验证的东西做的呢?

Do you not have an authenticated session associated with your requests? Are you populating some headers in order to do the authentication or are you using something similar to HTTP Basic Authentication to do it?

另外,如果你有302,是不是因为你首先是不认证,不就是不出所料其中一个有效的302?这是很重要的,因为如果你在第一时间并没有进行身份验证,你跟着GET请求需要进行反正认证。

Also, if you got a 302, was it because you were not authenticated in the first place or is it a valid 302 which is as expected? This is important because if you were not authenticated in the first place, your followed GET request needs to be authenticated anyway.

这篇关于AJAX重定向的困境,如何让重定向URL或如何设置重定向请求属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆