我有一個簡單的函數,它只會將已翻譯的消息從服務器返回給客戶端。但是結果顯示未定義,當我將結果傳遞給var時。jQuery Ajax導致未定義
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;
}
});
}
結果 - >未定義(壞)
當我看着頁眉它給了我:
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"
爲什麼我仍然得到一個不確定的結果呢?
你正試圖從回調函數返回。其中一種方法是在'success'塊中使用響應來執行你的代碼 –
當你在你內部輸入'console.dir(msg)'時,控制檯說什麼'success:function(msg)'? –
當我將代碼放入成功塊中時,它完美無缺。我試圖讓代碼更加可重用。 – Colin