gpt4 book ai didi

javascript - 如何在数据类型为 'text' 的 jQuery 中执行跨域 ajax?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:11 24 4
gpt4 key购买 nike

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

  • 是否可以使用数据类型 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 调用。我创建了 new question对于那个。在我看来,我必须选择@zzzz 的选项 b)。

最佳答案

要进行跨域请求,您的选择非常有限。作为@Mrchief提到,你可以做server side proxyjsonp .

另一个选项是 Cross-Origin Resource Sharing (CORS) , W3C 工作草案。引自 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.

您可以在 this site 上找到一些实例.

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

关于javascript - 如何在数据类型为 'text' 的 jQuery 中执行跨域 ajax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7157455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com