AJAX不处理302重定向 [英] AJAX not handling 302 redirect

查看:3526
本文介绍了AJAX不处理302重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想查询一个外部服务器上的数据库。要做到这一点,我将我的本地服务器(Tomcat)的,并创建一个AJAX调用(只是XMLHtt prequest对象 - 我没有使用任何JavaScript库)到页面后附加了查询。粘贴完全相同的网址到Firefox导致它尝试下载一个XML文档。我的目标是使用AJAX来获取XML文档。

I have a database on an external server that I am trying to query. To do this, I am going on my local server (Tomcat) and creating an AJAX call (just the XMLHttpRequest object - I am not using any JavaScript libraries) to the page with a query appended. Pasting the exact same URL into Firefox causes it to try to download an XML document. My goal is to use AJAX to get that XML document.

我遇到的问题是,当我做与AJAX调用,萤火显示,GET响应返回302暂时移动用一个红色的X旁边。为GET响应的头与OAuth的授权位置参数,当我复制和粘贴的位置参数,它需要我到正确的页面(告诉我下载的XML对象)。

The problem I am having is that when I make the call with AJAX, Firebug shows that the GET response returned 302 "Moved Temporarily" with a red X next to it. The header for the GET response has a Location parameter with OAuth authorization, and when I copy and paste the location parameter it takes me to the correct page (tells me to download the XML object).

编辑:我试图使用jQuery的 $得到(URL,功能(数据){警报(数据)}); 而同样的事情发生了 - 毫无戒备,但一个红色的GET请求和302萤火虫

I tried it using jQuery's $.get("URL", function(data){alert(data)}); and the same thing happened - no alert, but a red GET request and 302 in Firebug.

根据这些信息,我认为数据库我打电话是先尝试重定向我到一些OAuth的东西,然后返回与访问数据库授权的URL。这是我应该用它来调用数据库,获取XML对象返回,然后做我的事。 AJAX似乎并没有能够处理重定向,并且不是崩溃。

Based on this information, I think that the database I am calling is first trying to redirect me to some OAuth thing, which then returns an authorized URL with which to access the database. This is what I should use to call the database, get the XML object back, and then do my thing. AJAX doesn't seem to be able to handle the redirect and is instead crashing.

我不知道这是正确的,但是,因为我尝试使用下面的code:

I'm not sure this is correct, however, because I tried using the following code:

  else if (xmlhttp.readyState == 4 && xmlhttp.status == 302){
  alert("Hello 302!");
}
else {
  document.getElementById("test").innerHTML = "On state: " + xmlhttp.readyState + "<br />HTTP Status: " + xmlhttp.status;
}

和它没有给我一个警告 - 相反,它表明,它是在状态4和状态0。我不明白为什么它会返回状态0。(编辑:固定在回答1并没有什么提到的错字改变)

and it didn't give me an alert - instead it shows that it is on state 4 and status 0. I don't understand why it would return status 0. ( Fixed the typo mentioned in answer 1 and nothing changed)

所以,我的问题是:

  • 什么,到底是怎么回事?
  • 什么是0的状态,为什么萤火虫给我的控制台一个X旁边302,为什么是不是有重定向?
  • 我该如何解决这个问题?
  • 有一次,我做修复它,我就可以获取该XML文件,还是有别的东西,我需要做什么?

编辑与更新:这是一个跨站点脚本问题。我去外部服务器上,然后跑了完全相同的文字,并能够检索和分析包含查询结果的XML文档。唯一的障碍是搞清楚如何从外部服务器做到这一点。我能够访问外部服务器的配置,将被研究如何操纵它,以允许经由来自其它站点的数据库查询的访问。

EDIT WITH UPDATE: It's a cross-site scripting issue. I went on the external server and ran the exact same script and was able to retrieve and parse an XML document containing the result of the query. The only obstacle is figuring out how to do this from an external server. I have access to the configuration of the external server and will be researching how to manipulate it to allow access via database queries from other sites.

推荐答案

由于它是你无法从其他域获取数据的Ajax请求:的 http://en.wikipedia.org/wiki/Same_origin_policy

Since it's an ajax request you can't pull data from another domain: http://en.wikipedia.org/wiki/Same_origin_policy

所有你可以在这里做的真的是从你自己的服务器(同一个域)的请求数据,并把它拉从外部数据库的数据给你。

All you can do here really is request data from your own server (same domain) and have it pull data from the external db for you.

这篇关于AJAX不处理302重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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