Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An alternative to XMLHttpRequest?

I am writing a Firefox extension. I am using a content script that matches a specific domain. I want to get data from a PHP page. Eventually adding a CSS file to the page to change the styling. The content of the PHP page will be the name of the CSS file I fetch. Here is my content script javascript, the alert returns nothing.

I was told I am limited by the same origin policy. Is there any way I can get data from the php page?

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();
like image 848
Jacques Blom Avatar asked Feb 29 '12 17:02

Jacques Blom


Video Answer


2 Answers

You're looking for JSONP.

like image 177
SLaks Avatar answered Sep 27 '22 18:09

SLaks


You can use Fetch API its not supported by Safari or IE for now but its a good alternative.

You ma use it as:

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?
}

You can get more info from MDN Using Fetch API. You may want More advance usage.

Update: you might find this article useful That's so fetch!

like image 40
Mustafa Dwekat Avatar answered Sep 27 '22 20:09

Mustafa Dwekat