我有一個特殊的問題從ajax響應獲取整數。無論何時我調用下面的代碼,parseInt(data)都會返回NaN,儘管數據是一個字符串。ParseInt不工作jQuery的ajax響應
function poll() {
$.ajax({
type: "GET",
dataType: "html",
url: 'images/normal/' + userId + '/' + saveCode + 'progress.txt',
error: function() {poll();},
success: function(data) {
// Change the text
$('#loading_text').html(data + '% complete');
// Change the loading bar
max = 357;
current_percent = parseInt(data); // returns NaN
$('loading_bar').width(Math.round(max * (current_percent/100)));
// Call the poll again in 2 seconds
if (loaded != true)
{
setTimeout(poll, 2000);
}
}
});
}
在螢火的typeof(數據)是串和數據=「89」(或從另一個1-100號)但它仍然是不工作。任何線索?
typeof(+數據)返回數字,但只是+數據本身返回NaN – Lobe 2009-12-16 05:07:24
你可以做alert(data.length)嗎?我認爲在「89」之前你有一些非數字。 – YOU 2009-12-16 05:21:16
這使現在更有意義。 data.length = 211.這可能是由於我的php代碼更新了文本文件105次以提供進度更新,所以它沒有正確清理文件。什麼是解決這個問題的最好方法?我認爲修剪功能會起作用 – Lobe 2009-12-16 06:06:07