Search code examples
ajaxjsoncross-domaingreasemonkeyscriptish

Making requests from userscript to the localhost


So, I'm trying to make cross-site AJAX request from my own script to the localhost. In the userscript (running on Firefox's Scriptish engine) I'm loading my script like this

myscript_include.setAttribute('src', 'http://localhost/myscript.js?' + Math.random());
head.appendChild(myscript_include);

It works indeed. Then, in myscript.js, I try to read data from localhost (finally, I would like to make get-post requests to scripts on my localhost to add any needed functionality to the web-page without writing actual Firefox extension).

Following instructions on making cross-site AJAX requests I add to myscript.js:

$.getJSON('http://localhost/ajaxdata.json', function(json) { 
    alert(json.message); 
});

Firefox JS console shows that GET request was actually made, and status is 200 OK. It even shows Content-Length 39, which is true indeed, but Response field remains empty and alert isn't shown!

What's wrong with that construction (except of it's horrible itself)? Is there some way to do what I want?


Solution

  • Not sure, but maybe adding Access-Control-Allow-Origin headers to localhost will solve this?

    ref: https://developer.mozilla.org/En/HTTP_access_control