简体   繁体   English

XMLHttpRequest HTTP标头

[英]XMLHttpRequest HTTP Headers

I'm trying to make a little script to rehost pictures on the web on imgur. 我正在尝试编写一些脚本来在imgur上重新托管网络上的图片。

This is called Image Sideloading and you only need to point the browser to http://api.imgur.com/2/upload?url= + Picture's Url 这称为“图像侧面加载”,您只需将浏览器指向http://api.imgur.com/2/upload?url= +图片的网址

It doesn't return any XML or JSON response so I can't parse it to get the img URL. 它不返回任何XML或JSON响应,因此我无法解析它以获取img URL。

But I think I found a way to do this but can't get it to work properly. 但是我认为我找到了一种方法来执行此操作,但无法使其正常工作。 Here's the code I'm using. 这是我正在使用的代码。

$(document).ready(function() {
    $("#button").click(function() {
        str = $("input").val().toString();
        link = "http://api.imgur.com/2/upload?url=" + str;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", link);
        xhr.send();
        var headers = xhr.getAllResponseHeaders().toLowerCase;
        alert(headers);

    });

});

And looking at Google Chrome's console these are the results generated after the script runs. 并查看Google Chrome浏览器的控制台,这些是脚本运行后生成的结果。

I am not allowed to post images yet so here's a link to the results: http://i.imgur.com/xCyIP.png 我还不能发布图像,所以这里是结果的链接: http : //i.imgur.com/xCyIP.png

I need to somehow access that 4th response header because even though this method doesn't return any parsable XML or JSON response that link is the uploaded img's URL which is all I need. 我需要以某种方式访问​​第4个响应标头,因为即使此方法未返回任何可解析的XML或JSON响应,该链接也是我所需的已上传img的URL。

So is there a way to access that info? 那么有没有办法访问该信息? Why is it cancelled? 为什么取消?

Thanks everyone! 感谢大家!

Well, the API must return something, else it is useless. 好的,API必须返回一些东西,否则它是无用的。

Actually, http://api.imgur.com/examples advises you to do something like: 实际上, http: //api.imgur.com/examples建议您执行以下操作:

var xhr = new XMLHttpRequest();
xhr.open("GET", link);
xhr.onload = function() {
    var url = JSON.parse(xhr.responseText).upload.links.imgur_page;
    alert(url);
}
xhr.send();

Edit. 编辑。 OK, I got it. 好,我知道了。 Normally, the above code should work, but I think imgur is facing a problem. 通常,上面的代码应该可以工作,但是我认为imgur面临问题。 I will report the bug. 我将报告该错误。

While the bug is still there, here is a dirty solution to serve your needs: 当错误仍然存​​在时,这里有一个肮脏的解决方案可以满足您的需求:

var xhr = new XMLHttpRequest();
xhr.open("GET", link);
xhr.onload = function() {
    html = xhr.responseText;
    alert(html.substr(html.indexOf('http://i.imgur.com/') + 19, 5));
}
xhr.send();

You code wasn't working because you weren't waiting for the answer. 您的代码无效,因为您没有等待答案。 You have to catch it through a callback function. 您必须通过回调函数来捕获它。

Edit. 编辑。 The above code only works locally. 上面的代码仅在本地工作。 As you're using jQuery, let me introduce the shortcut $.post : 当您使用jQuery时,让我介绍一下$.post快捷方式:

$(document).ready(function() {
    $("#button").click(function() {
        $.post('http://api.imgur.com/2/upload.json', {
            key: 'ec575de603b936e54c2b4a9f232d537e',
            image: $("input").val().toString()
        }, function(data) {
            alert(data.upload.image.hash);
        });
    });
});​

http://jsfiddle.net/Le_Sphinx/rEfdS/ http://jsfiddle.net/Le_Sphinx/rEfdS/

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

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