jQuery的阿贾克斯导致不确定 [英] jQuery Ajax results in undefined
本文介绍了jQuery的阿贾克斯导致不确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单功能只从服务器向客户机返回一个翻译的消息。但结果表明不确定的,当我通过结果变成变种。
I have a simple function which only returns a translated message from the server to the client. But the result shows undefined, when I pass the result into a 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);
当我看着页眉它给了我:
When I look into the Headers it gives me:
Server ASP.NET Development Server/9.0.0.0
Date 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"
为什么我仍然得到一个不确定的结果?
Why do I still get an undefined result?
推荐答案
您可以传递一个回调函数的 MessageNoResult
功能
You can pass a callback function to the MessageNoResult
function
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的阿贾克斯导致不确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文