﻿/*
ajax，$.jbecool核心之一。
需要jquery/jquery.json
author:jerry
history:
v1.0 jerry ajax基本功能，同步模式的send


*/
(function ($) {
    $.jbecool.ajax = {};
    /*
    使用方法：
    var result = $.jbecool.ajax.send("某个C#静态方法",{参数名:参数值});

    resultType表示返回的对象类型，默认情况下会将返回值转换为JSON对象，
    你也可以设定为xml，来表示返回的是xml/html内容
    url表示采用其他的基于BeCool CMS 2的网站的Bridge.aspx文件

    注意，如果是无参数函数，应该这样写：
    $.jbecool.ajax.send('BeCool.AMS.ArticleControl.GetList4', {});
    因为jquery中的ajax，如果data默认是undefine，那么就不会提交任何数据

    */
    $.jbecool.ajax.send = function (func, data, resultType, url) {
        var res;
        $.ajax({
            async: false,
            type: "POST",
            url: (url == null || url == undefined) ? $.jbecool.randomUrl("/BeCool/Api.aspx") : url,
            data: data,
            global: false,
            processData: false,
            beforeSend: function (xhr, settings) {
                var toPost = "___functionName=" + func + "&";
                if (!$.jbecool.isNullOrUndefine(settings.data)) {
                    var paras = settings.data;
                    for (var p in paras) {

                        //encodeURIComponent防止内容中包含&的问题，而&是jquery post作为分隔参数用的
                        if (typeof (paras[p]) == "object") {
                            toPost += p + "=" + encodeURIComponent($.toJSON(paras[p])) + "&";
                        } else {
                            toPost += p + "=" + encodeURIComponent(paras[p]) + "&";
                        }
                    }
                }
                toPost = $.jbecool.removeEndWith(toPost, "&");

                settings.data = toPost;
            },
            success: function (result) {
                //这个地方是否可以采用unescape 来解决
                if (resultType == "xml") {
                    res = result;
                    res = res.replace(/\\u003c/g, "<");
                    res = res.replace(/\\u003e/g, ">");
                    res = res.replace(/\\\"/g, "\"");
                    res = res.replace(/\\t/g, "\t");
                    res = res.replace(/\\n/g, "\n");
                    res = res.substr(1);
                    res = res.substr(0, res.length - 2);
                }
                else {
                    if (result == null || result == undefined || result == "")
                        res = "";

                    try {
                        res = $.evalJSON(result);
                    } catch (e) {
                        res = result;
                    }

                    //指定了返回的对象类型
                    if (resultType != undefined && resultType != null) {
                        var res2 = new resultType();
                        for (var p in res2) {
                            //日期类型做特殊处理（$.evalJSON将日期类型转换为/Date(1308651512250)/
                            if (res2[p] instanceof Date) {
                                if (res[p] == null || res[p] == undefined) {
                                    res2[p] = null;
                                    continue;
                                }
                                var tmp2 = Number(res[p].replace("/Date(", "").replace(")/", ""));
                                if (isNaN(tmp2))
                                    res2[p] = null;
                                else
                                    res2[p] = new Date(tmp2);
                            } else {
                                res2[p] = res[p];
                            }
                        }
                        res = res2;
                    }
                }
            }
        ,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                res = new $.jbecool.InterMsg();
                res.errorMsg = "$.jbecool.ajax.send 方法出错！" + XMLHttpRequest.responseText;
                res.isSuccess = false;
            }
        });
        return res;
    };

})(jQuery);

$(function () {
    $.blockUI.defaults.css = {};
});
