问题描述
我正在尝试通过 AJAX请求从另一个域中获取图像的二进制数据.我尝试了各种方法,但是没有可行的解决方案.我在互联网上发现了一些看起来不错的代码,但是即使调用此命令,我仍然会出错.
I am trying to get binary data of an image from another domain with an AJAX request. I tried various methods, but there was no working solution. I found some code on the internet that looked good, but even with this calls I get errors.
我怎么了?有标准化的方法可以做到这一点吗?
What do I wrong? Is there a standardized way to do this?
这是我到目前为止尝试过的内容:
Here is what I tried until now:
var request = this.createCORSRequest('GET', 'http://url/to/image.png');
request.onload = function () {
var text = request.response;
};
request.onerror = function (error) {
alert('Woops, there was an error making the request.');
};
request.send();
private createCORSRequest(method, url) {
var xhr: XMLHttpRequest = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
var xdhr = new XDomainRequest();
xdhr.open(method, url);
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
我什至在stackoverflow上找到了没有ajax的解决方案,但是它对我不起作用:
I even found this solution without ajax here on stackoverflow, but it does not work for me:
以下是错误事件包含的属性的屏幕:
Here a screen of the properties the error event contains:
我的目标是从我从原子供稿中获得的url获取图像的二进制文件.我需要二进制文件才能将图片复制到MS SharePoint.
My goal is to get the binary of an image from a url which I get from an atom feed . I need the binaries to copy the picture to MS SharePoint.
推荐答案
除非以下条件,否则您无法从其他域获取数据:
You cannot get data from another domain unless :
- 远程服务器允许它使用CORS
- 您以不安全的模式运行浏览器.
原因:否则,站点A可能(恶意)从站点B读取用户数据
Reason : otherwise site A would be able to (maliciously) read the user data from site B
这篇关于使用AJAX请求从另一个域获取图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!