1

我正在使用 Greasemonkey 脚本从 RSS 提要http://www.instapaper.com/folder/48337/rss/11185/QBV0RZfH4KBO7GwgrR3D8b7sv90中提取数据并将其插入到带有 jQ​​uery 的页面中。该地址返回有效的 RSS,但当 Greasemonkey 触发 Ajax 调用(见下文)时,它总是返回一个空请求并触发错误回调。我在 Ajax 调用中缺少什么?

$.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>");
  }
});

我不太确定是不是这样,但我的jQuery.ajax调用似乎发出了与我简单地将 RSS URL 粘贴到我的位置栏时不同类型的 HTTP 调用。即使我指定请求是“GET”,jQuery 似乎正在发送“OPTIONS”请求。下面是我的 Ajax 请求发出的调用和通过在地址栏中输入 URL 的工作请求的实时 HTTP 标头捕获的输出 -

这个使用空 (?) 响应对象触发 Ajax 错误回调:

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

这成功地在浏览器中显示数据(当我在地址栏中放置 URL 时会发生什么):

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
4

3 回答 3

1

我正在将 jQuery 加载到 unsafeWindow 对象中,所以我猜测 Greasemonkey 触发 OPTIONS 请求的问题与不允许使用jQuery 使用的XMLHttpRequest有关。

jQuery.ajax通过从使用切换到使用常规 GreasemonkeyGM_xmlhttpRequest调用,我能够成功发出 GET 。我仍然在响应回调中使用 jQuery。下面是我对注释掉的旧行所做的一个简短示例。

//$.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...
    }
});

(我在 Stack Overflow 问题[Load remote URL with Greasemonkey and jQuery][1]中找到了我的问题的部分答案。)

于 2009-08-28T15:03:09.623 回答
0

我相信您必须在 data 参数中将空数据集传递给服务器。使用JSON,您可以使用 来做到这一点data: "{}",但我不确定您的具体情况是什么。如果您不向它传递一组空数据,我认为它永远不会成功完成。

于 2009-08-26T18:24:50.653 回答
0

错误的原因是同源策略。它只允许您对自己的域执行 XMLHTTPRequests。

(GM_xmlhttpRequest 允许您域外的请求,请务必添加://@grant GM_xmlhttpRequest)

在这里查看答案:

jQuery $.ajax(), $.post 在 Firefox 中将“OPTIONS”作为 REQUEST_METHOD 发送

于 2016-04-17T09:38:57.527 回答