简体   繁体   English

成功调用ajax函数

[英]Calling a function on ajax success

I have to populate data from SQL to jvectormap, using ajax and httphandler. 我必须使用ajax和httphandler将数据从SQL填充到jvectormap。

I can get the json to populate the jvectormap but I dont know how to pass the data which I got from ajax 我可以获取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');
                    }
                }
            });
        })

I want to pass dataC from ajax to var CountryData and then to the jvectormap function. 我想将dataC从ajax传递到var CountryData,然后传递给jvectormap函数。

The data I get is in json format 我得到的数据是json格式

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

In your way function you code after ajax call will be called before ajax call completion asi it is asynchronous 以您的方式,您在ajax调用之后编写的代码将在ajax调用完成之前被调用,因为它是异步的

you can call function like this: 您可以这样调用函数:

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

Your function outside ajax call which will be called: 在ajax调用之外的函数将被调用:

function MyFunction(data)
{

// do something here


}

in your case: 在您的情况下:

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');
                    }
                }
            });
        })

}

Simply call the function in the ajax call: success: function (data) { countryCall(data); 只需在ajax调用中调用该函数即可:success:function(data){countryCall(data); }, },

founction countryCall(data) {

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM