调用ajax成功的函数

时间:2014-05-27 12:01:26

标签: c# javascript jquery ajax

我必须使用ajax和httphandler将数据从SQL填充到jvectormap。

我可以让json填充jvectormap,但我不知道如何传递我从ajax获取的数据

$.ajax({
            url: 'CountryRegistrations.ashx',
            type: 'POST',
            success: function (data) {
                var dataC = data;
            },
            error: function (data) {
                alert("Error");
            }
        });

 var countryData = [];
        //for each country, set the code and value
        $.each(dataC.countries, function () {
            countryData[this.Country] = this.Count;
        });
        $( function () {
            //World map by jvectormap
            $('#world-map').vectorMap({
                map: 'world_mill_en',
                backgroundColor: "#fff",
                regionStyle: {
                    initial: {
                        fill: '#e4e4e4',
                        "fill-opacity": 1,
                        stroke: 'none',
                        "stroke-width": 0,
                        "stroke-opacity": 1
                    }
                },
                series: {
                    regions: [{
                            values: countryData,
                            scale: ["#3c8dbc", "#2D79A6"], //['#3E5E6B', '#A6BAC2'],
                            normalizeFunction: 'polynomial'
                        }]
                },
                onRegionLabelShow: function (e, el, code) {
                    var country = $.grep(dataC.countries, function (obj, index) {
                        return obj.Country == code;
                    })[0];
                    if (country != undefined) {
                        el.html(el.html() + ': ' + country.Count + ' new visitors');
                    }
                }
            });
        })

我想将dataC从ajax传递给var CountryData,然后传递给jvectormap函数。

我得到的数据是json格式

"countries":[{"Country":"AE","Count":5},{"Country":"CH","Count":2},{"Country":"IN","Count":3},{"Country":"PK","Count":3},{"Country":"US","Count":2}]

2 个答案:

答案 0 :(得分:2)

以你的方式运行ajax调用之后的函数将在ajax调用完成之前调用,因为它是异步的

你可以这样调用函数:

success: function (data) {
                MyFunction(data);
            }

您在ajax调用之外的函数将被调用:

function MyFunction(data)
{

// do something here


}

在你的情况下:

function MyFunction(data)
{
var countryData = [];
        //for each country, set the code and value
        $.each(data.countries, function () {
            countryData[this.Country] = this.Count;
        });
        $( function () {
            //World map by jvectormap
            $('#world-map').vectorMap({
                map: 'world_mill_en',
                backgroundColor: "#fff",
                regionStyle: {
                    initial: {
                        fill: '#e4e4e4',
                        "fill-opacity": 1,
                        stroke: 'none',
                        "stroke-width": 0,
                        "stroke-opacity": 1
                    }
                },
                series: {
                    regions: [{
                            values: countryData,
                            scale: ["#3c8dbc", "#2D79A6"], //['#3E5E6B', '#A6BAC2'],
                            normalizeFunction: 'polynomial'
                        }]
                },
                onRegionLabelShow: function (e, el, code) {
                    var country = $.grep(dataC.countries, function (obj, index) {
                        return obj.Country == code;
                    })[0];
                    if (country != undefined) {
                        el.html(el.html() + ': ' + country.Count + ' new visitors');
                    }
                }
            });
        })

}

答案 1 :(得分:0)

只需在ajax调用中调用该函数:     成功:函数(数据){                 countryCall(数据);             },

founction countryCall(data) {

}