【发布时间】: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