我有一个jquery post函数从php代码块返回一个数据对象。
$.post("get_chat_history.php", {
item_id : item_id,
participant_1 : participant_1,
participant_2 : participant_2
}, function(data) {
alert(data);
return data;
});
这是从下面的函数在另一个叫JavaScript文件
var data = getChatHistory(current_member_id,item_global["user"] ["member_id"],item_global["id"]);
alert(data);
现在里面的$。员额警报(数据)抛出JSON格式的正确的价值观,但是当我测试相同的值返回给调用函数,我得到未定义的值。
有什么我失踪了,因为我想保持这个函数的通用性和从其他地方调用?
问候,
Sapatos
Ajax是**异步**(** A **表示异步)。你为什么认为你必须提供回调?如果你可以简单地*返回一个值,那么'$ .post'可以直接返回这个值,你不需要回调。 – 2012-08-13 11:26:01
[Javascript函数与JQuery POST总是返回undefined]的可能重复(http://stackoverflow.com/questions/1400854/javascript-function-with-jquery-post-always-returns-undefined) – Dennis 2012-08-13 11:26:07
检查出相关的问题。有几个类似的问题应该证明是有帮助的。 – Dennis 2012-08-13 11:26:56