码迷,mamicode.com
首页 > Web开发 > 详细

取得 Ajax 返回参数

时间:2017-01-17 08:06:02      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:post   callback   set   something   json   this   status   data   spec   

1  ajax 函数,p1 为正常函数

    function ExecWebFunction(callback,p1) {
     
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: URL,
            data: ajaxdata,
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                //成功执行语句
                if (typeof callback === function) {
                    callback("SUCCESS", data.d);
                }
                getresult(data.d);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //错误执行语句
                if (typeof callback === function) {
                    callback("ERROR", textStatus);
                }

            }
        });
    }

2 callback 函数--用来处理上面执行之后的事务.

    function callback(info, result) {
        if (info == "SUCCESS") {
            //do somethings
        }
        else {
          //do somethings
        }
    }

3 调用方式如下:
ExecWebFunction(callback,p1);

 

取得 Ajax 返回参数

标签:post   callback   set   something   json   this   status   data   spec   

原文地址:http://www.cnblogs.com/BinBinGo/p/6291635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!