jQuery Ajax始终返回&“未定义&"?
问题描述:
function getThisFrame(frameId) {
var r;
$.ajax({
type: "POST",
contentType: "application/json",
url: "abcdefg.asmx/RetriveThis",
data: "{Id:" + Id + "}",
dataType: 'json',
success: function (result) {
return result.d
}
});
}
返回值总是未定义"?我该如何解决?谢谢!
The return value is always "undefined"? How could I solve this? Thanks!
数据肯定没问题!
答
您要将 result.d
返回到 $.ajax()
,而不是返回 getThisFrame()
.
you are returning result.d
to $.ajax()
not to getThisFrame()
.
如果要以某种方式处理 result.d
,则需要某种回调.
You need somekind of callback if you want to handle result.d
somehow.
function getThisFrame(frameId, callback) {
var r;
$.ajax({
type: "POST",
contentType: "application/json",
url: "abcdefg.asmx/RetriveThis",
data: "{Id:" + Id + "}",
dataType: 'json',
success: function (result) {
if(typeof callback === 'function') callback.apply(this, [result.d]);
}
});
}
getThisFrame(5, function(data){
// do something with data.
});