【问题标题】:An alternative to XMLHttpRequest?XMLHttpRequest 的替代方案?
【发布时间】:2012-02-29 17:46:40
【问题描述】:

我正在编写一个 Firefox 扩展。我正在使用与特定域匹配的内容脚本。我想从 PHP 页面获取数据。最终将 CSS 文件添加到页面以更改样式。 PHP 页面的内容将是我获取的 CSS 文件的名称。这是我的内容脚本 javascript,警报不返回任何内容。

有人告诉我,我受到同源政策的限制。有什么方法可以从 php 页面获取数据?

function getData() {
            client = new XMLHttpRequest();
            try{
                client.open('GET','http://localhost:8888/istyla/login/popuplogin/myaccount.php');                   
            } catch (e){
                alert( "error while opening " + e.message );
            }

            client.onreadystatechange = function(){
                if (client.readyState ==4){
                        user_data = client.responseText;
                        var currenttheme = user_data;
                        alert (currenttheme);
                }
            }

            client.send(null);
}

getData();

【问题讨论】:

标签: javascript firefox firefox-addon firefox-addon-sdk


【解决方案1】:

您正在寻找JSONP

【讨论】:

    【解决方案2】:

    您可以使用 Safari 或 IE 目前不支持的 Fetch API,但它是一个不错的选择。

    你可以把它用作:

    function getData(){
        fetch('flowers.jpg').then(function(response) {
             return response.blob();
        })
    }
    
    
    if(self.fetch) {
        // run my fetch request here
        var resp = getData();
    } else {
        // do something with XMLHttpRequest?
    }
    

    您可以从MDN Using Fetch API 获得更多信息。你可能想要More advance usage

    更新:您可能会发现这篇文章很有用That's so fetch!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 2015-03-03
      • 2015-09-25
      • 2019-12-16
      • 2011-06-20
      • 2015-11-03
      相关资源
      最近更新 更多