【问题标题】:Calling a function on ajax success在ajax成功时调用函数
【发布时间】:2014-05-27 12:01:26
【问题描述】:

我必须使用 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}]

【问题讨论】:

    标签: c# javascript jquery ajax


    【解决方案1】:

    在你的方式函数中,你在 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');
                        }
                    }
                });
            })
    
    }
    

    【讨论】:

      【解决方案2】:

      只需在 ajax 调用中调用函数即可: 成功:函数(数据){ 国家电话(数据); },

      founction countryCall(data) {
      

      }

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 1970-01-01
        • 2013-08-04
        • 2021-01-11
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2015-07-12
        相关资源
        最近更新 更多