1

I'm using a Greasemonkey script to pull data from the RSS feed http://www.instapaper.com/folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90 and insert it into a page with jQuery. The address returns valid RSS, but when Greasemonkey fires an Ajax call (see below) it always returns an empty request and fires the error callback. What am I missing in my Ajax call?

$.ajax({
  url: "http://www.instapaper.com/folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90",
  dataType: "xml",
  data: "",
  type: "GET",
  success: function (data){
    var classString = "";

    $("item",data).each(function (i) {
      classString = classString + "<li><a href='" + $("link",this).text() + "'>" + $("title",this).text() + "</a></li>";
    });

    var message = "<div id='classes'><h2>Class Links</h2><ul>" + classString + "</ul></div>";
    $("#sidelinks").prepend(message);
  },
  error: function (XMLHttpRequest, textStatus, errorThrown) {
    $("#sidelinks").prepend("<div id='classes'><h2>Class Links</h2><p>There was an error accessing class link information.</p></div>");
  }
});

I'm not quite sure if this is it, but my jQuery.ajax call seems to issue a different type of HTTP call than when I simply paste the RSS URL into my location bar. Even though I specify that the request is to be a "GET", jQuery seems to be sending an "OPTIONS" request. Below is the output trapped with Live HTTP Headers for the call issued by my Ajax request and for the working request by entering the URL into the location bar--

This one fires the Ajax error callback with an empty (?) response object:

http://www.instapaper.com/folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90

OPTIONS /folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90 HTTP/1.1
Host: www.instapaper.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive
Origin: http://home.bju.edu
Access-Control-Request-Method: GET
Access-Control-Request-Headers: x-requested-with

HTTP/1.x 200 OK
Date: Thu, 27 Aug 2009 15:16:53 GMT
Server: Apache
P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
Vary: Accept-Encoding
Content-Type: text/xml; charset=utf-8
Content-Length: 1210
Age: 0

This successfully displays data in the browser (what happens when I drop URL in address bar):

http://www.instapaper.com/folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90

GET /folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90 HTTP/1.1
Host: www.instapaper.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Proxy-Connection: keep-alive

HTTP/1.x 200 OK
Date: Thu, 27 Aug 2009 15:18:04 GMT
Server: Apache
P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
Vary: Accept-Encoding
Content-Type: text/xml; charset=utf-8
Content-Length: 1210
Age: 0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Duncan Johnson
  • 274
  • 4
  • 13

3 Answers3

1

I was loading jQuery into an unsafeWindow object, so I'm guessing that my problem with Greasemonkey firing OPTIONS requests relates to it not being allowed to use the XMLHttpRequest that jQuery uses.

I was able to successfully issue a GET by switching from using jQuery.ajax to using a regular Greasemonkey GM_xmlhttpRequest call. I still use jQuery in the response callbacks. Below is an abbreviated example of what I'm doing with the old lines commented out.

//$.ajax({
GM_xmlhttpRequest({
    url: "http://www.instapaper.com/folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90",
    //dataType: "xml",
    //data: "",
    //type: "GET",
    method: "GET",
    //success: function (data){
    onload: function (responseObject){
          var data = responseObject.responseText;

          // Same code as in my question, use some jQuery selectors here to parse the data...
    },
    //Error: function (XMLHttpRequest, textStatus, errorThrown) {
    onerror: function () {
      // Same code as in my question...
    }
});

(I found part of the answer to my question in Stack Overflow question [Load remote URL with Greasemonkey and jQuery][1].)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Duncan Johnson
  • 274
  • 4
  • 13
0

I believe you have to pass an empty data set to the server in the data parameter. Using JSON, you can do that with data: "{}", but I'm not sure what that would be in your case specifically. If you don't pass it an empty set of data, I don't think it will ever complete successfully.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Max Schmeling
  • 12,363
  • 14
  • 66
  • 109
  • Well, I tried that, but no success. Any other ideas out there? – Duncan Johnson Aug 26 '09 at 19:34
  • What exactly did you try? "{}" would not work for XML I'm assuming... maybe just an empty string? – Max Schmeling Aug 27 '09 at 17:14
  • I tried "{}" and empty string "" (which is what I have in edited question above). I'm not sure what's going on, unless it has to do with the difference between HTTP GET and HTTP OPTIONS. – Duncan Johnson Aug 27 '09 at 17:40
  • Try POST instead of GET. it shouldn't matter, but who knows – Max Schmeling Aug 27 '09 at 18:04
  • Didn't make a difference. It still fires an OPTIONS. However, when I dropped this code in Ubiquity it fired a GET and seems to have grabbed the data. The original context for this call is a Greasemonkey script where I'm including jQuery with some other code. Maybe this is a Greasemonkey bug. – Duncan Johnson Aug 27 '09 at 20:09
  • It is a Greasemonkey problem! The same code that works in Ubiquity (i.e., issues a GET) fails in Greasemonkey (it issues an OPTIONS). I give up for today. – Duncan Johnson Aug 27 '09 at 20:41
0

The reason for the error is the same origin policy. It only allows you to do XMLHTTPRequests to your own domain.

(the GM_xmlhttpRequest allows requests outside your domain be sure to add: // @grant GM_xmlhttpRequest)

See the answer here:

jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST_METHOD in Firefox

Community
  • 1
  • 1