如何在 jQuery 中使用 dataType 'text' 进行跨域 ajax?

发布于 2024-11-30 22:41:03 字数 1256 浏览 0 评论 0原文

在我的 javacript 函数中,我称之为 ajax。它工作正常,但仅当我从 firebird 服务器访问网页时。我的测试服务器上有相同的代码。 ajax 要求下载一些文件,但只有 firebird 服务器有其 ip 向我们的客户端注册,以便能够在那里进行 scp。如果我从测试服务器访问 php 文件,我需要执行相同的操作。所有服务器都在内网内。

  • 是否可以使用 dataType text 来做到这一点?
  • 我需要在服务器端进行任何更改吗?

ajax 调用:

url = "https://firebird"+path+"/tools.php?";    

jQuery.ajax({
    type: 'get',
    dataType: 'text',
    url: url,
    data: {database: database_name, what: 'download', files: files, t: Math.random() },
    success: function(data, textStatus){
        document.getElementById("downloading").innerHTML+=data;
    }
});

更新 1

我的小 Web 应用程序恢复数据库,以便我可以对它们进行测试。现在我想增强它,以便我可以连接到我们的客户并下载特定的备份。我们的客户只允许 firebird 服务器连接到他们的网络。但我有自己的服务器专用于测试。因此,每次我想要下载数据库时,我都需要连接 firebird。我的 Web 应用程序的源代码和包含所有备份的文件夹均安装在 firebirdtesting 服务器上的同一位置。现在我的解决方案(用于下载)可以工作,但只能从 firebird 进行。不过,我基本上只工作测试服务器。

更新 2

我进行了两次 ajax 调用。一种是纯 jQuery 调用(我想我可以对此应用任何解决方案),另一种是来自 jsTree 的 ajax 调用。我为此创建了新问题。在我看来,我必须选择@zzzz 的选项 b)。

In my javacript function I call this ajax. It works fine but only when I access the web page from firebird server. I have the same code on my testing server. The ajax asks to download some files but only firebird server has its ip registers with our clients to be able to scp there. I need to do the same if I access the php files from testing server. All the servers are inside intranet.

  • is it possbile to use dataType text to do so?
  • do I need to do any changes on the server side?

ajax call:

url = "https://firebird"+path+"/tools.php?";    

jQuery.ajax({
    type: 'get',
    dataType: 'text',
    url: url,
    data: {database: database_name, what: 'download', files: files, t: Math.random() },
    success: function(data, textStatus){
        document.getElementById("downloading").innerHTML+=data;
    }
});

Update 1

My little web application restores databases so I can do my testing on them. Now I want to enhance it so I can connect to our customers and download a particular backup. Our customer allowed only firebird server to connect to their networks. But I have my own server dedicated to testing. So every time I want to download a database I need to connect firebird. The source of my web application and the folder with all backups are mounted into the same location on both servers firebird and testing. Right now my solution (for downloading) works but only from firebird. I work basically only testing server though.

Update 2

I make two ajax calls. One is pure jQuery call (I guess I can apply any solution to this one) and the other one is ajax call from jsTree. I created new question for that one. I seems to me that I have to go for @zzzz's option b).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜访吸血鬼 2024-12-07 22:41:03

要执行跨域请求,您的选择相当有限。正如@Mrchief提到的,你可以做服务器端代理jsonp

另一种选择是跨源资源共享 (CORS),这是一项 W3C 工作草案。引用自此博客文章

CORS 背后的基本思想是使用自定义 HTTP 标头来允许
浏览器和服务器足够了解彼此
确定请求或响应应该成功还是失败。

对于一个简单的请求,使用 GET 或 POST 且没有自定义的请求
headers 和 body 是 text/plain,请求以
称为 Origin 的额外标头。 Origin 标头包含原点
请求页面的(协议、域名和端口),以便
服务器可以轻松确定是否应该提供响应。

您可以在此网站上找到一些实例。

您需要对服务器端进行更改以接受 CORS 请求。由于您可以控制服务器,因此这应该不是问题。 CORS 的另一个缺点是,它可能与旧版浏览器不兼容。因此,如果您的一些重要受众使用不兼容的浏览器,那么服务器端代理实际上可能是您更好的选择。

To do cross domain requests, your options are fairly limited. As @Mrchief mentioned, you could do server side proxy and jsonp.

Another option is Cross-Origin Resource Sharing (CORS), a W3C working draft. Quoting from this blog post:

The basic idea behind CORS is to use custom HTTP headers to allow both
the browser and the server to know enough about each other to
determine if the request or response should succeed or fail.

For a simple request, one that uses either GET or POST with no custom
headers and whose body is text/plain, the request is sent with an
extra header called Origin. The Origin header contains the origin
(protocol, domain name, and port) of the requesting page so that the
server can easily determine whether or not it should serve a response.

You can find some live examples on this site.

You will need to make changes to the server side, to accept the CORS requests. Since you have control over the server, this shouldn't be a problem. Another downside with CORS is that, it might not be compatible with older browsers. So, if some of your essential audiences use incompatible browsers, the server side proxy may actually be a better option for you.

枉心 2024-12-07 22:41:03

我只是想提供一个替代方案。

我不太确定您的网络设置,但如果您可以访问 DNS,那么如果您只为您的服务器提供同一域的任意子域,也许最简单。例如用于 Web 前端的 www.foo.com 和用于 firebird 服务器的 firebird.private.foo.com 。这样就变成跨子域而不是跨域了。然后在两个页面上的 JavaScript 中的某个地方,

document.domain = "foo.com";

这位先生实现了这个解决方案 这里

I just want to offer an alternative.

I am not too sure regarding your network setup, but if you have access to the DNS, maybe it would be easiest if you just give your servers some arbitrary subdomain of the same domain. Something like www.foo.com for the webfront and firebird.private.foo.com for the firebird server. This way, it becomes cross subdomain instead of cross domain. Then somewhere in your JavaScript on both pages,

document.domain = "foo.com";

This gentleman achieved this solution here.

浅唱ヾ落雨殇 2024-12-07 22:41:03

您有以下选择

:a)您使用 jsonp 类型作为数据类型,但这涉及在服务器端进行更改以将数据作为 json 而不是 txt 传回。此更改可能与

{
  "text":<your current text json encoded> 
}

您的 js 端 一样简单你用它作为response.text;话虽如此,如果您从 sm 其他域获取文件的文本,我不确定您更改代码有多容易。

b) 另一种选择是您在服务器上(即在您的域内)编写一个处理程序/端点,该处理程序/端点将向第三个域发出 HTTP 请求来获取文件,然后您将文件发送回您的客户端,现在您的客户端将有效地与您的会话进行对话。仅域,您可以控制一切。因为大多数 yoyr 问题都基于 ruby​​,这里是一个示例:

req = Net::HTTP.get_response(URI.parse('http://www.domain.com/coupons.txt'))

@play = req.body

您可以找到有关相同内容的更多详细信息 此处。

希望这会有所帮助。

You have the following options with you

a) You use jsonp type as your datatype but this involves making changes on the server side to pass the data back as json and not as txt.. this change might be as simple as

{
  "text":<your current text json encoded> 
}

and on your js side you use this as response.text; Having said that if you are getting the textis for you file from sm other domain I am not sure how easy it is for you to change the code.

b) The other option is you write a handler/end point on your server i.e within your domain that will make an HTTP request to this third domain gets the file and you send the file back to your client and effectively now your client talks to your domain only and you have control over everything. as most of yoyr questions are based on ruby here is an example:

req = Net::HTTP.get_response(URI.parse('http://www.domain.com/coupons.txt'))

@play = req.body

you can find more details about the same here.

Hope this helps.

向日葵 2024-12-07 22:41:03

另一个想法是使用您的网络服务器作为代理。您将需要考虑此路由的安全影响。

Another idea is to use you web server as a proxy. You will need to consider the security implications for this route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文