【问题标题】:Jquery and cometd problems with URL and 404 errors带有 URL 和 404 错误的 Jquery 和 Cometd 问题
【发布时间】:2011-01-30 21:47:22
【问题描述】:

我正在为 cometd 服务器编写 jquery 客户端(我正在使用 jquery.cometd.js 插件),但我想不出为什么最简单的情况不起作用。

cometd 服务器在 apache 后面(所以它在同一个域上运行)并且所有 请求从 uri http://wwwhost/cometd 转发。

问题是当我尝试连接(通过执行握手())到 cometd 时,它没有发送请求 直接到 /cometd 但到 /cometd/handshake 会出现 404 错误。 我检查了我正在测试的其他应用程序,dojo 总是连接到 /cometd,然后发送消息“握手”。

有人知道 jquery.cometd 为什么要这样做吗?

这是我在 apache 日志中可以看到的:

- - [23/Mar/2010:17:59:30 +0100] "POST /cometd/handshake HTTP/1.1" 404 158 "http://wwwhost/" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100308 Iceweasel/3.5.8 (like Firefox/3.5.8)"

您可以在下面找到我正在使用的代码(它或多或少是我从示例中得到的)。

(function($)
{
        var COMETD_URL = "http://wwwhost/cometd";
        var cometd = $.cometd;

        $(document).ready(function() {

                cometd.configure({
                        url: COMETD_URL,
                        logLevel: 'debug'
                });

                cometd.handshake();

        });
})(jQuery);

和萤火虫调试:

Initial transport is Object {}
cometd.js (line 278)
Status disconnected -> handshaking
cometd.js (line 278)
Handshake sent Object { version="1.0", more...}
cometd.js (line 278)
Send Object { url="http://wwwhost/cometd/handshake", more...}
cometd.js (line 278)
POST http://wwwhost/cometd/handshake
POST http://wwwhost/cometd/handshake
404 Not Found 104ms

编辑

看起来我的服务器实现不支持非 cometd 的 URI。 Jquery 在末尾添加消息的类型,因此在发送握手时将其发送到: /cometd/handshake 通常看起来像 /cometd/message_type。

我在 cometd.js 代码中找到了发送消息的函数,该函数有三个参数:

function _send(messages, longpoll, extraPath)

这个函数被调用例如:

 _send([message], true, 'connect');

这意味着我将始终以 /cometd/handshake 结尾。 我必须修复服务器或注释掉 cometd.js 中的附加 url。

【问题讨论】:

    标签: jquery http-status-code-404 comet cometd


    【解决方案1】:

    我遇到了同样的事情。它在 maven 下运行时有效,但不能直接在码头中运行。

    我向我的码头添加了一个名为 contexts/cometd.xml 的文件。这似乎是多余的,但它对我有用。

      <?xml version="1.0"  encoding="ISO-8859-1"?>
      <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"   "http://jetty.mortbay.org/configure.dtd">
      <configure class="org.eclipse.jetty.webapp.WebAppContext">
        <Set name="contextPath">/</Set>
        <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/server</Set>
      </configure>
    

    【讨论】:

      【解决方案2】:

      尝试将appendMessageTypeToURL 设置为 false

      cometd.configure({
          url: COMETD_URL,
          logLevel: 'debug',
          `appendMessageTypeToURL`: false
      });
      

      但正如文档所说,握手失败可能还有另一个原因

      握手可能会失败几次 原因:

      • 您输入了错误的服务器 URL
      • 无法成功协商长轮询传输
      • 服务器拒绝握手(例如,身份验证 凭据错误)
      • 服务器崩溃
      • 出现网络故障

      【讨论】:

      • A 在任何地方都看不到 appendMessageTypeToURL 参数。我检查了来源,它不存在。服务器 URL 正确。我拥有连接到服务器的所有权利。唯一的问题是额外的路径。
      猜你喜欢
      • 2015-12-14
      • 2018-01-16
      • 2011-02-24
      • 2011-05-08
      • 2016-02-05
      • 1970-01-01
      • 2014-03-29
      • 2014-05-24
      • 1970-01-01
      相关资源
      最近更新 更多