jQuery-创建通用的ajax函数 [英] jquery - creating a generic ajax function

查看:100
本文介绍了jQuery-创建通用的ajax函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jQuery Ajax始终返回“ undefined&rdquo ;?

Possible Duplicate:
jQuery Ajax always returns “undefined”?

我正在尝试提出一个通用的jquery ajax函数,该函数采用url&参数并返回结果.我试图避免使用async:false,因为它会锁定浏览器.如果我在ajax调用中使用success回调,由于计时问题,返回的数据为null(成功不等待直到返回数据).如果我使用complete,则persons对象在LoadPersons方法中仍然为null,因为它不等待ajax调用返回数据.如果将alert(persons)放在complete回调中,则会得到[object][Object],所以我要取回数据.如何解决"此问题?我说的有道理吗?理想情况下,我还要显示一个正在加载.."图像.

I'm trying to come up with a generic jquery ajax function which takes the url & parameters and returns the result. I'm trying to avoid using async:false as it locks up the browser. If I use success callback in the ajax call, the data returned is null due to the timing issue (success doesn't wait until the data is returned). If I use complete, the persons object is still null in the LoadPersons method as it doesn't wait for the data to be returned from the ajax call. If place an alert(persons) in the complete callback, I get [object][Object] so I'm getting the data back. How do I "fix" this issue? Am I talking sense? I would also ideally like to show a "loading.." image while it's doing this.

这是我的代码-

  <script type="text/javascript">
    $(function() {
                var persons;
                var urlGetPersons = "Default.aspx/GetPersons";

                LoadPersons();

                function LoadPersons() {
                    persons = CallMethod(urlGetPersons, { });
                    if (persons != null) {
                        // do something with persons.
                    }
                }

                function CallMethod(url, parameters) {
                    var data;
                    $.ajax({
                        type: 'POST',
                        url: url,
                        data: JSON.stringify(parameters),
                        contentType: 'application/json;',
                        dataType: 'json',
                        success: function(result) {
                            data = result.d;
                        }, // use success?
                        complete: function(result) {
                            data = result.d;
                        } // or use complete?
                    });
                    return data;
                }
            });
   </script>

推荐答案

通用"就是我的想法-但它仍然是异步的:

This is what's comes to my mind with "generic" - but it's still asynchronous:

function CallMethod(url, parameters, successCallback) {
                //show loading... image

                $.ajax({
                    type: 'POST',
                    url: url,
                    data: JSON.stringify(parameters),
                    contentType: 'application/json;',
                    dataType: 'json',
                    success: successCallback,
                    error: function(xhr, textStatus, errorThrown) {
                        console.log('error');
                    }
                });
            }

CallMethod(url, pars, onSuccess);

function onSuccess(param) {
    //remove loading... image
    //do something with the response
}

这篇关于jQuery-创建通用的ajax函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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