为什么jQuery ajax只能在Chrome中工作,而不能在Firefox或IE中工作?

时间:2021-11-11 16:53:59

I've tried a few flavors of jQuery ajax. $.ajax , $.getJSON & $.get all work great in Chrome. However none work in Firefox or IE. I'm reading a json file from the same directory. The json originates from desk.com's api. I'm using php & cURL to grab & save the json.

我尝试了几种jQuery ajax。美元。ajax,美元。getJSON & $。让所有的工作在铬伟大。但是,在Firefox或IE中都不能工作。我正在从同一个目录读取一个json文件。json源自desk.com的api。我使用php和cURL来获取和保存json。

In $.ajax I've also tried defining the dataType Firebug gives no errors. console.log the ajax return in firebug and it returns an Object with all of the json data but not displaying to page. When alerting it gives [object Object]

在美元。我还尝试过定义dataType Firebug,它不会产生错误。控制台。在firebug中记录ajax返回,它返回一个对象,其中包含所有json数据,但不显示给页面。当报警时,它会发出[对象]

JS:

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);

webpage:

网页:

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

THE JSON [ filename = 491244.json ]

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"
            }

        ]
    }
}

2 个解决方案

#1


1  

Probably that's because of the condition

可能那是因为条件

   if(difference < 604800000){

That varies because of the inconsistency in both date object and the current time milles as well.

由于日期对象和当前时间milles的不一致,所以会有所不同。

Check with that by debugging or just get rid of that for some time.

通过调试或者在一段时间内摆脱它。

#2


2  

You should use $.getJSON if you want it to parse the JSON automatically. And resp[_embedded][entries] should be resp._embedded.entries or resp["_embedded"]["entries"]. When you don't quote the names, it uses them as variables, but they have no values.

您应该使用美元。getJSON如果您希望它自动解析JSON。resp[_嵌入式][条目]应该是。_嵌入式的。条目或职责(“_embedded”)(“条目”)。当不引用名称时,它将它们用作变量,但它们没有值。

#1


1  

Probably that's because of the condition

可能那是因为条件

   if(difference < 604800000){

That varies because of the inconsistency in both date object and the current time milles as well.

由于日期对象和当前时间milles的不一致,所以会有所不同。

Check with that by debugging or just get rid of that for some time.

通过调试或者在一段时间内摆脱它。

#2


2  

You should use $.getJSON if you want it to parse the JSON automatically. And resp[_embedded][entries] should be resp._embedded.entries or resp["_embedded"]["entries"]. When you don't quote the names, it uses them as variables, but they have no values.

您应该使用美元。getJSON如果您希望它自动解析JSON。resp[_嵌入式][条目]应该是。_嵌入式的。条目或职责(“_embedded”)(“条目”)。当不引用名称时,它将它们用作变量,但它们没有值。