简体   繁体   English

使用JQuery通过AJAX获取文件

[英]Get a file through AJAX with JQuery

I looked for an answer to my problem everywhere but I didn't found what I want to know. 我到处寻找问题的答案,但我找不到我想知道的东西。

Here is what I want to do: I have an url of an image, pdf or html page from the same domain and from other domains too. 这是我想要做的:我有一个来自同一域和其他域的图像,pdf或html页面的URL。 Is it possible to "download" them with AJAX? 是否可以使用AJAX“下载”它们? But I don't want to have them physically on my server or anywhere else, I just want to use them directly on my website without having to upload them again (to save time). 但是我不想让它们在我的服务器上或其他任何地方,我只是想直接在我的网站上使用它们而不必再次上传它们(以节省时间)。

I'm also having some troubles doing "XMLHttpRequest.getAllResponseHeaders()" when the content comes from an other domain. 当内容来自其他域时,我也遇到了“XMLHttpRequest.getAllResponseHeaders()”的麻烦。 I tried with multiples url. 我尝试使用倍数网址。 I received nothing: "". 我什么也没收到:“”。 Is it normal? 这是正常的吗?

Thank you for your help ! 谢谢您的帮助 ! :) :)

Unfortunately it is impossible to make AJAX requests for resources on domains external to the website's host domain, edit: unless they implement a workaround, like CORS or JSONP . 遗憾的是,无法对网站主机域外部域上的资源进行AJAX请求,编辑: 除非他们实施变通方法,如CORS或JSONP I recommend doing some reading on Cross Origin Resource Sharing for more details. 我建议对跨源资源共享进行一些阅读以获取更多详细信息。

As for resources on the same domain, you'd be storing those on your server anyway - you'd have to, in fact, because by its very nature it would be a part of your site. 至于同一域上的资源,无论如何你都要将它们存储在你的服务器上 - 事实上,你必须这样做,因为它本质上就是你网站的一部分。 If you describe what you're trying to implement specifically (perhaps post a jsfiddle with your code), we could find a solution to your specific use case. 如果您描述了您正在尝试专门实现的内容(也许可以使用您的代码发布jsfiddle ),我们可以找到针对您的特定用例的解决方案。 Hope this helps. 希望这可以帮助。

[...] image, pdf or html page [...] [...]图像,pdf或html页面[...]

The types of these requests are very different. 这些请求的类型非常不同。 What you mean with 'download' is a little ambigious. 你对'下载'的意思是有点暧昧。 If you're requesting a file via AJAX, this loads the value into a variable. 如果您通过AJAX请求文件,则会将值加载到变量中。 It doesn't save the file to your computer. 它不会将文件保存到您的计算机。

[...] I received nothing: "" Is it normal? [...]我什么都没得到:“”这是正常的吗? [...] [...]

Yes, if the remote server isn't the same as you're currently on (also the same port) and also doesn't support CORS (cross-domain requests), the result will be blank. 是的,如果远程服务器与您当前所在的(也是相同的端口)不同,并且也不支持CORS(跨域请求),则结果将为空。

If it's not possible to alter the remote server's cross-domain policies [and you have the permission to use their services], you could write a PHP script on your server that acts as a proxy. 如果无法更改远程服务器的跨域策略[并且您有权使用其服务],则可以在服务器上编写充当代理的PHP脚本。 But don't forget to secure it to prevent mis-use. 但是不要忘记保护它以防止误用。 Also keep in mind that such a script can raise your traffic dramatically, so this should only be "Plan B". 另外请记住,这样的脚本可以显着提高您的流量,因此这应该只是“B计划”。

You certainly can fetch images and html pages with AJAX and display them on your webpages. 您当然可以使用AJAX获取图像和html页面并将其显示在您的网页上。

The issues you are having with the xmlHttpRequest.GetAllResponseHeaders is most likely caused by the same origin policy . 您使用xmlHttpRequest.GetAllResponseHeaders时遇到的问题很可能是由相同的原始策略引起的。 The browser prohibits you from interacting with the loaded data from another domain. 浏览器禁止您与来自其他域的加载数据进行交互。

The server you are fetching the data from has to explicitly indicate that it allows your domain to fetch and display his resources by setting the Access-Control-Allow-Origin response header to that of your domain. 您从中获取数据的服务器必须明确指出它允许您的域通过将Access-Control-Allow-Origin响应头设置为域的响应来获取和显示其资源。

A way arround this is by using a proxy and setting the header yourself. 一种方法是使用代理并自己设置标题。

The following boilerplate code ( basically taken from the jquery api page ) loads your gravatar image ( delivered in png format ) from the stackoverflow website into a callback parameter. 以下样板代码(基本上来自jquery api页面)将您的gravatar图像(以png格式传递)从stackoverflow网站加载到回调参数中。 As others have noted, the same origin policy applies to ajax requests ( The sample code therefore can onlybe successfully run from the stackoverflow site context, eg. by opening a js console on this page ). 正如其他人所指出的那样,相同的源策略适用于ajax请求(因此示例代码只能从stackoverflow站点上下文成功运行,例如通过在此页面上打开js控制台)。

$.ajax({
  method: "GET"
  , url: "https://www.gravatar.com/avatar/d85e0ad5243245aabe2d34a3c9a296a9?s=32&d=identicon&r=PG&f=1"
  , cache: false
  , beforeSend: function( xhr ) {
    xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  }
})
  .done(function( data ) {
    if ( console && console.log ) {
      console.log( "Length of data:", data.length );
      console.log( "Sample of data:", data.slice( 0, 100 ) );
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM