jQuery Ajax始终返回&“未定义&"? [英] jQuery Ajax always returns "undefined"?

查看:93
本文介绍了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.
});

这篇关于jQuery Ajax始终返回&“未定义&"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆