【问题标题】:Why does jQuery ajax work in Chrome but not Firefox or IE?为什么 jQuery ajax 可以在 Chrome 中运行,但不能在 Firefox 或 IE 中运行?
【发布时间】:2015-10-23 14:31:54
【问题描述】:

我尝试了几种 jQuery ajax。 $.ajax , $.getJSON & $.get 在 Chrome 中都很好用。 但是在 Firefox 或 IE 中都没有。我正在从同一目录中读取一个 json 文件。 json 来源于desk.com 的api。我正在使用 php 和 cURL 来抓取和保存 json。

$.ajax 中,我也尝试过定义dataType Firebug 没有给出错误。 console.log ajax 在 firebug 中返回,它返回一个包含所有 json 数据但不显示到页面的对象。 发出警报时,它会给出 [object Object]

JS:

function getDesk($group_number)
{   
    now = Date();
    unixTimeMS = Date.parse(now);  
    var tempHtml = '<table class="table table-bordered" id="desk_data_table">'+
        '<thead style="background-color:#eee">'+
        '<tr>'+
        '<th>Available</th>'+
        '<th>Name</th>'+
        '<th>Created</th>'+
        '<th>Updated</th>'+
        '<th>Current login</th>'+
        '<th>Last login</th>'+
        '</tr>'+
        '</thead>'+
        '<tbody id="desk_data_table_body">';
    $url = $group_number + ".json";
    $.get( $url, function( resp ) {
        $.each( resp['_embedded']['entries'], function( key, value ) {
            updated = value['updated_at'].replace(/[TZ+]/g, " ");
            updatedKeep = value['updated_at'].replace(/[TZ+]/g, " ");
            updated = Date.parse(updated);
            difference = unixTimeMS - updated;
            if(difference < 604800000){
                lastLogin = value['last_login_at'].replace(/[TZ+]/g, " ");
                created = value['created_at'].replace(/[TZ+]/g, " ");
                updated = value['updated_at'].replace(/[TZ+]/g, " ");
                current = value['current_login_at'].replace(/[TZ+]/g, " ");

                if(value.available == true){
                    tempHtml += "<tr style='background-color:#9de7a2;'><td >" + value['available'] + "</td>";
                } else {
                    tempHtml += "<tr style='background-color:#f0a0a0;'><td >" + value['available'] + "</td>";
                }

                tempHtml += "<td nowrap='nowrap'> " +value['name']+"</td>";
                tempHtml += "<td nowrap='nowrap'> " + created + "</td>";
                tempHtml += "<td nowrap='nowrap'> " + updatedKeep + "</td>";
                tempHtml += "<td nowrap='nowrap'> " + current + "</td>";
                tempHtml += "<td nowrap='nowrap'> " + lastLogin + "</td></tr>"; 
            }
            else{ false; }
        });
        tempHtml += "</tbody></table>";
        $('#' + $group_number).html(tempHtml);
    });
}

getDesk(491244);

网页:

<div id="491244"></div>

JSON [文件名 = 491244.json]

{
    "_embedded": {
        "entries": [
            {
                "avatar": "http://www.gravatar.com/avatar/26536",
                "available": false,
                "created_at": "2014-04-08T19:10:41Z",
                "current_login_at": "2015-10-21T14:21:27Z",
                "email": "Matthew.Jamison@email.com",
                "email_verified": true,
                "id": 21912353,
                "last_login_at": "2015-10-19T20:50:22Z",
                "level": "siteadmin",
                "name": "Matt Jamison",
                "public_name": "Matt Jamison",
                "updated_at": "2015-10-21T14:21:27Z"
            }

        ]
    }
}

【问题讨论】:

  • 改掉在函数中使用全局变量的习惯,它们是问题的常见原因。使用var 关键字来声明你的变量。我想知道其中一个变量是否与某些东西发生冲突,尝试将它们全部设为本地变量,看看是否有帮助。
  • 你试过单步调试器中的回调函数吗?
  • 使用 var 关键字将所有内容更改为局部变量。而且我尝试过调试器,但我是新手,所以我不完全理解它。你在 codementor.io 上吗?

标签: jquery json ajax google-chrome firefox


【解决方案1】:

如果您希望它自动解析 JSON,您应该使用 $.getJSONresp[_embedded][entries] 应该是 resp._embedded.entries 或 resp["_embedded"]["entries"]。当您不引用名称时,它会将它们用作变量,但它们没有值。

【讨论】:

  • 我已经在 Chrome 中成功使用了$.getJSON。它在 Firefox 中不起作用。
  • 它不可能像你在问题中写的那样工作。您需要在这些属性名称周围加上引号。
  • 除非你有全局变量_embedded = '_embedded'; entries = 'entries';
  • 如果这些功能在 Firefox 中不起作用,成千上万的网站将无法运行。所以一定是你正在做的事情造成干扰。如果您的应用程序没有更多代码,就不可能知道您做错了什么。能否提供该网站的链接?
  • 我已经用我实际使用的代码更新了它。我认为问题出在 $.each @Barmar
【解决方案2】:

可能是条件的原因

   if(difference < 604800000){

由于日期对象和当前时间千分之一的不一致,这会有所不同。

通过调试检查或暂时摆脱它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多