我目前有一个正常工作的ajax调用,它正在获取所需的数据,但是当我需要它返回一个字符串时,它会以对象的形式返回数据。我不确定我在这里做错了什么,但这是我的代码:
var pillid=drawpills();
pillid=pillid.toString();
alert(pillid); //output is [object Object]
和ajax方法是:
function drawpills() {
return $.post('js/fetchdata.php', function (data) {
});
}
drawpills().done(function(data) {
var clock = document.getElementById('clock');
clock.innerHTML = "<img src='images/clock/pill.png' alt='pill_image' id='pillpic" + data + "'/>";
var picid = "pillpic" + data;
return data;
}).fail(function() {
// an error occurred
});
编辑:
function drawpills() {
return $.post('js/fetchdata.php', function (data) {
});
}
drawpills().done(function(data) {
var clock = document.getElementById('clock');
clock.innerHTML = "<img src='images/clock/pill.png' alt='pill_image' id='pillpic" + data + "'/>";
callback(data);
}).fail(function() {
// an error occurred
});
function callback(data){
var picid = "pillpic" + data;
return picid;
}