【问题标题】:Why does this jQuery AJAX PUT work in Chrome but not FF为什么这个 jQuery AJAX PUT 可以在 Chrome 中工作,但不能在 FF 中工作
【发布时间】:2013-05-18 07:22:54
【问题描述】:

在 Chrome 中,这就像它应该做的那样执行 HTTP PUT,但在 FireFox 21 中却没有。 javascript 控制台或后端没有错误。

这是 HTML:

<div id="createTeamModal" class="small reveal-modal">
        <form id="createTeamForm">
            <div class="row"><p id="teamFlavorText" class="lead">Building a new team</p></div>
            <div class="row">
                <div class="small-4 large-4 columns"><label>Team Name:</label></div>
                <div class="small-6 large-6 columns"><input name="teamName" id="teamName" type="text" size="20"/></div>
            </div>
            <div class="row"><p class="lead">Does this team work for a business?</p></div>
            <div class="row">
                <div class="small-4 large-4 columns"><label>Business Size:</label></div>
                <div class="small-6 large-6 columns">
                    <select id="businessSizeSelect" name="businessSizeSelect">
                    <option value="1">Nope, I work alone</option><option value="2">2 to 49</option><option value="3">50 to 99</option><option value="4">100 to 999</option><option value="5">1,000+</option>
                    </select>
                </div>
            </div>
            <div id="businessLocationDiv" class="row" style="display: none; margin-top: 20px;">
                <div class="small-4 large-4 columns"><label>Business Location:</label></div>
                <div class="small-6 large-6 columns">
                    <select id="businessLocationSelect" name="businessLocationSelect">
                    </select>
                </div>
            </div>
            <div id="businessTypeDiv" class="row" style="display: none; margin-top: 20px;">
                <div class="small-4 large-4 columns"><label>Industry:</label></div>
                <div class="small-6 large-6 columns">
                    <select id="businessTypeSelect" name="businessTypeSelect">                      
                    </select>
                </div>
            </div>
            <div class="row" style="margin-top: 20px;">
                <div class="large-offset-10 small-1 large-1 columns">
                    <button id="createTeamButton" class="small button">Create</button>
                </div>
            </div>
        </form>
        <a class="close-reveal-modal">&#215;</a>
    </div>

这里是 jQuery:

$("#createTeamButton").click(function () {
    var teamObject = new Team();
    teamObject.description = $("#teamName").val();
    teamObject.businessSize = $("#businessSizeSelect").val();
    teamObject.businessType = $("#businessTypeSelect").val();
    teamObject.businessLocation = $("#businessLocationSelect").val();

    $.ajax({
        type: "PUT",
        url: "/ajax/rest/team",
        dataType: "json",
        data: JSON.stringify(teamObject),
        success: function () {
            // Reload the team select box
            loadTeamSelectBox();

            // Pop up the site create modal
            $('#createSiteModal').foundation('reveal', 'open');
        },
        error: ajaxErrorHandler
    });
});

我在 Fiddler 中观察到了它们,工作 (Chrome) 和不工作 (Firefox) 之间的区别在于 HTTP PUT 在 Chrome 中触发而在 Firefox 中不触发。

现在,我知道并非所有浏览器都保证 jQuery.ajax PUT。

我读过

这些网站重申 PUT 可能无法在所有浏览器中运行,但应该在 FF 中运行。

最后,我用 FF21 和 PUT 实现了以下目标

我当然可以解决这个问题,但在我看来这应该可行。我宁愿不 jerry-rig 一些东西,而是让 jQuery 的 .ajax 正常工作。

其他细节: * jQuery 版本 2.0.0 * 后端是 Spring3

[编辑以添加 HTML]

【问题讨论】:

  • 我认为它不会在所有浏览器中都支持。但这里很少有类似的链接stackoverflow.com/questions/5846741/…stackoverflow.com/questions/5894400/…
  • 如果您认为这是一个 jQuery 错误,您应该提供您正在使用的 jQuery 的确切版本。但我不认为它与PUT 有任何关系,因为 URL 也发生了变化。
  • 我更新了问题以包含所要求的详细信息。事实证明 URL 没有改变。我看到的 GET 是由 Chrome 和 Firefox 完成的。我今天看到的区别在于,工作(Chrome)实际上执行 HTTP PUT,而失败(Firefox)不执行 HTTP PUT。
  • 适用于我的 Firefox 21.0 和 jQuery 2.0.2。您的问题不包括问题的根源。 PLBKAC?

标签: jquery ajax firefox xmlhttprequest http-put


【解决方案1】:

这是一个令人失望的答案。按钮单击正在提交表单,即使它不一定要这样做。我把 onsubmit="return false;"在表格中,问题得到解决。

【讨论】:

    【解决方案2】:

    您没有指定要发送到服务器的内容类型。我有一个类似的问题,服务器甚至没有尝试从查询中读取数据,因为它不知道提供的格式是什么,所以它只是忽略了它。

    当为 jQuery 请求指定 dataType 时,你只是告诉 jQuery 服务器应该用什么格式回复你。要告诉服务器您发送的是什么数据格式,您必须提供contentType

    $.ajax({
        type: "PUT",
        url: "/ajax/rest/team",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(teamObject)
    }).done(function() {
        // Reload the team select box
        loadTeamSelectBox();
    
        // Pop up the site create modal
        $('#createSiteModal').foundation('reveal', 'open');
    
    }).fail(ajaxErrorHandler);
    

    Backbone.js 有一个不错的 RESTful API,您可以使用 as reference

    【讨论】:

      【解决方案3】:

      当我转到 google.com 并按 f12(firebug)然后运行以下代码时,我无法在 windows 上重新生成您对 firefox 21.0 的声明:

      var s = document.createElement("script");
      s.src="http://code.jquery.com/jquery-1.9.1.js";
      document.body.appendChild(s);
      //allow some time to load and then run this
       $.ajax({
        type: "PUT",
        url: "/search",
        dataType: "json",
        data: JSON.stringify({hi:"there"}),
        success: function (data) {
          console.log(data);
        },
        error: function(e) { 
          console.log(e); 
        }
        });
      

      我得到响应 405 方法不允许,但更重要的是;在控制台中打开连接的详细信息时,我可以看到 PUT,而不是发布。

      当我转到http://jqueryui.com/autocomplete/#remote 并使用 url:"/resources/demos/autocomplete/search.php" 运行代码(不必包含 JQuery)时,xml 成功结束并且 firebug 显示 PUT 请求。

      我不知道有哪个网站可以测试服务器端代码是否检测到 PUT 请求(Google 也拒绝 POST,因此它可能是 POST),但从 Firebug 报告的外观来看,它正在发送 PUT 请求。

      [更新]

      我可以确认,在 Firefox 21.0 上,上述代码 100% 确定发出 PUT 请求。刚刚使用 .net 应用对其进行了测试,Chrome 27.0.1453.94 FF 都设置了 PUT 请求。

      【讨论】:

        【解决方案4】:

        我认为您可能遗漏了其他内容 - 这个 jsFiddle 在最新的 Chrome 和 Firefox 21 上执行 PUT 请求:

        http://jsfiddle.net/QKkQg/

             var teamObject = new Object();
            teamObject.description = $("#teamName").val();
            teamObject.businessSize = $("#businessSizeSelect").val();
            teamObject.businessType = $("#businessTypeSelect").val();
            teamObject.businessLocation = $("#businessLocationSelect").val();
        
            $.ajax({
                type: "PUT",
                url: "/ajax/rest/team",
                dataType: "json",
                data: JSON.stringify(teamObject),
                success: function () {
                    // Reload the team select box
                    loadTeamSelectBox();
        
                    // Pop up the site create modal
                    $('#createSiteModal').foundation('reveal', 'open');
                },
                error: function() { }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-24
          • 1970-01-01
          • 2017-07-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-28
          • 1970-01-01
          • 2012-11-03
          相关资源
          最近更新 更多