1

I'm trying to retrieve the online status of users from a chat client called IMVU(the regular little image holder by the profile pictures isn't enough, I'm making something bigger, so I need some signal), and the way to do that is use this line of the user ID 1111111 for example: http://avatars.imvu.com/catalog/web_status_updater.php?ol=1&list=1111111

It returns a php file containing a line of JSON. I need that whole line of text put into a javascript variable so I can use it. I need to use this in a script I'm making, but I can't seem to get it to work. I've tried lots of things, the closest seems to be this one:

function readTextFile(file)
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                alert(allText);
            }
        }
    }
    rawFile.send(null);
}


readTextFile("http://avatars.imvu.com/catalog/web_status_updater.php?ol=1&list=1111111");

(I'll change the alert(allText); to return allText; or return rawFile.responseText; when I get this experiment to work first and make sure that the text is actually stored and displayed.)

What happens is that the alert shows up blank. Just a white box, that's all. My prior attempts had the box show up and it said "undefined", but now it's doing something I guess? Why is it blank though? And how do I fix it?

EDIT: It works in IE but not Firefox apparently.

4
  • Have you tried another web browser and are you using the right URL and does the URL actually return data? Commented Jan 23, 2016 at 21:08
  • Just a silly question, why not use jQuery? Commented Jan 23, 2016 at 21:10
  • @Mike Oh dear God, it works in IE but not Mozilla. It was driving me nuts for a while. Commented Jan 23, 2016 at 21:13
  • @Xorifelse I did try to use .load(I'm guessing that's what you mean?), but it didn't work, now I'm thinking it might, but the issue might be Firefox. Commented Jan 23, 2016 at 21:14

1 Answer 1

3

I believe the problem here is the Same Origin Policy. You are attempting to make a request to a domain that is different than the one that originated the request.

Some websites specify headers which allow this, but the headers returned here are missing the Access-Control-Allow-Origin header.

One way around this is to use JSON-P on servers that support it.

Try this:

jQuery.ajax({
        url:"http://avatars.imvu.com/catalog/web_status_updater.php?ol=1&list=1111111", 
        dataType:"jsonp"
})
.done(function(data) {
        console.log(data)
});
Sign up to request clarification or add additional context in comments.

7 Comments

It works in Internet Explorer. I'm a bit confused, can you suggest a solution? I'd like to make it work in all browsers.
What version of IE? Since the policy is enforced by browsers, it could be that IE is not enforcing it, or allows it when the header is absent. The real solution would be for imvu to add the headers.
Version: 11.0.9600.18163
Is there any way I could somehow do another step in between to maybe put the text into an element somehow as a temporary fix and then get the value of the element?
I was thinking you might be able to fetch the page if you have PHP and echo it out. You could also check if their server supports JSON-P requests.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.