jQuery Ajax导致未定义
我有一个简单的函数,它仅将翻译后的消息从服务器返回到客户端。但是,当我将结果传递给var时,结果显示为未定义。
function MessageNoResult() { $.ajax(
{
type: "POST",
async: true,
url: '<%= ResolveUrl("~/WebMethods.aspx/MessageNoResult") %>',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
return msg.d;
}
});
}
var message = MessageNoResult();alert(message);
当我查看标题时,它会给我:
Server ASP.NET Development Server/9.0.0.0Date Wed, 09 Nov 2011 09:01:31 GMT
X-AspNet-Version 2.0.50727
Cache-Control private, max-age=0
Content-Type application/json; charset=utf-8
Content-Length 24
Connection Close
Response {"d":"No search result"}
JSON d "No search result"
为什么我仍然得到不确定的结果?
回答:
您可以将回调函数传递给该MessageNoResult
函数
function MessageNoResult(callback) { $.ajax(
{
type: "POST",
async: true,
url: '<%= ResolveUrl("~/WebMethods.aspx/MessageNoResult") %>',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
callback(msg);
}
});
}
然后调用:
MessageNoResult(function(msg) { alert(msg.d);
});
以上是 jQuery Ajax导致未定义 的全部内容, 来源链接: utcz.com/qa/406102.html