views:

992

answers:

4

I'm writing my first bit of jQuery and I'm having a problem with jQuery.get(). I'm calling this;

$.get(url, updateList);

where updateList is defined like so;

  function updateList(data)
  {
    if (data) 
    { 
      $('#contentlist').html(data); 
    }
    else
    {
      $('#contentlist').html('<li><a href="#" id="synclink">Nothing found. Try again</a></li>'); 
    }
  }

The function runs, and updateList is called. It works fine in IE. However, in Firefox, the data parameter is always empty. I would expect it to be filled with the content of the webpage I passed in as the URL. Am I using it wrong?

Notes;

  • in Firebug, I've enabled the Net panel, and I get the request showing up. I get a 200 OK. The Headers tab looks fine, while the Response and Html panels are both empty.
  • The page I'm trying to download is a straight html page -- there's no problem with server code.
  • The page with Javascript is local to my machine; the page I'm downloading is hosted on the net.
  • I've tried checking the url by copy-pasting it from my page into the browser -- it happily returns content.
  • The error occurs even in Firefox Safe Mode -- hopefully that rules out rogue addins.
+1  A: 

You'll most likely need to fix your page that you're quering with XHR because it should be returning content. Copy paste the link in the Firebug net tab and make a new tab, and edit that page with your text editor so it spits content back.

meder
The page I'm querying is a simple html fragment in a .html file. Browsing to it shows the content. Should I do anything else?
Steve Cooper
A: 

Stick alert (or breakpoint in Firebug) and see if the data returned is not an object (or if there is any data). If the former - you may need to drill into the object to get your markup

DroidIn.net
The data returned is empty; there is no object.
Steve Cooper
To the point that people already made - do you have your HTML you are querying on a different URL? If you have it on totally different domain you won't be able to bypass restrictions (unless you implement a proxy as shown here http://is.gd/1P5iw) if you have subdomain there's way of achiving it (see http://is.gd/1P5oz) Now - if you do $.ajax instead of $.get you can also have "onerror" callback - have you try that?
DroidIn.net
+2  A: 

Are you falling afoul of the single-origin policy? Firefox may be being stricter about what constitutes a different domain, or IE may be relaxing its policies as a result of administrative settings.

Also, what do you get if you try a Webkit browser (Chrome or Safari)?

DDaviesBrackett
valid concern, but wouldn't FF throw a security exception / access denied exception if that were the case?
Josh E
Chrome does indeed fail.
Steve Cooper
no, FF doesn't make a peep about returning blank responses from file:// URLs.
DDaviesBrackett
+5  A: 

You probably won't be able to do this due to cross domain security. IE will allow you to ajax remote domain when running from file://, but FF and Chrome wont.

try to put both files on the same server and see if it works (it should)

Nir Levy
Yeah. Once I moved from file protocol to http, it started to work. I just created a local site in IIS and accessed it as http://localhost/... and voila!
Steve Cooper