jQuery 运行 C# 方法

本文关键字:方法 运行 jQuery | 更新日期: 2023-09-27 18:33:31

谁能指出我正确的方向?

我正在尝试通过Javascript/jQuery从C#方法获取输出。

返回的输出[object Object]何时应返回首都。

jQuery(使用 jqvmaps 工具):

$(document).ready(function () {

    var Capital = null;
    jQuery('#vmap').vectorMap(
{
    map: 'world_en',
    backgroundColor: '#a5bfdd',
    borderColor: 'white',
    borderOpacity: 0.25,
    borderWidth: 1,
    color: 'gray',
    enableZoom: true,
    hoverColor: 'orange',
    hoverOpacity: null,
    normalizeFunction: 'linear',
    scaleColors: ['#b6d6ff', '#005ace'],
    selectedColor: 'red',
    selectedRegion: null,
    showTooltip: true,
    onRegionClick: function (element, code, region) {
        var codeValue = $('#Code').text();
        var countryCode = $('#Code').text();
        var data = null;
        data = { CountryCode: codeValue.toUpperCase() }

        console.log(data);

        $.ajax({
            type: "POST",
            url: 'Default.aspx/GetCapital',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (code) {
                Capital = code.d;
                CapitalCity.text(Capital);
            }
        });
        var ISOCode = $('#Code');
        ISOCode.text(code);
        var Country = $('#Country');
        Country.text(region);

        var CapitalCity = $('#Capital');


    }
});

    });

C#:

using System;
using System.Collections.Generic;
using System.Web.UI;
using Newtonsoft.Json;
using System.IO;
using System.Web.Services;
using System.Web.Script.Services;
namespace WorldMapDetails
{
    public partial class _Default : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetCapital(string CountryCode)
        {
            //do your stuff
            Country Country = new Country();
            List<Country> selectedCountry = JsonConvert.DeserializeObject<List<Country>>(File.ReadAllText(@"C:'Users'Brian'Documents'Bloxinations'Bloxinations'WorldMapInfo'WorldMapDetails'Scripts'json'Countries.json"));
            String Capital = null;
            foreach (Country c in selectedCountry)
            {
                if (c.cca2 == CountryCode)
                {
                    Capital = c.capital.ToString().ToUpper();
                }
            }
            return Capital;
        }
    }
}

如果我运行这个jQuery,它工作得很好,但首都文本的更改是"延迟"的(我必须双击一个地区)。

$(document).ready(function () {
    var Capital = $('#Capital');
    jQuery('#vmap').vectorMap(
    {
        map: 'world_en',
        backgroundColor: '#a5bfdd',
        borderColor: 'white',
        borderOpacity: 0.25,
        borderWidth: 1,
        color: 'gray',
        enableZoom: true,
        hoverColor: 'orange',
        hoverOpacity: null,
        normalizeFunction: 'linear',
        scaleColors: ['#b6d6ff', '#005ace'],
        selectedColor: 'red',
        selectedRegion: null,
        showTooltip: true,
        onRegionClick: function (element, code, region)
        {
            var codeValue = $('#Code').text();
            var countryCode = $('#Code').text();
            var data = { CountryCode:codeValue.toUpperCase() }
            var ISOCode = $('#Code');
            ISOCode.text(code);
            var Country = $('#Country');
            Country.text(region);

            $.ajax({
                type: "POST",
                url: 'Default.aspx/GetCapital',
                data: JSON.stringify(data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (code) {
                    Capital.text(code.d);
                    //console.log(data);
                    console.log(code);
                }
            });
        }

    });

});

如何修复它以使其立即更新(无需双击)?

jQuery 运行 C# 方法

修复将

ajax 结果分配给Capital变量,如下所示。

    $(document).ready(function () {
    var Capital = null;
    jQuery('#vmap').vectorMap(
{
    map: 'world_en',
    backgroundColor: '#a5bfdd',
    borderColor: 'white',
    borderOpacity: 0.25,
    borderWidth: 1,
    color: 'gray',
    enableZoom: true,
    hoverColor: 'orange',
    hoverOpacity: null,
    normalizeFunction: 'linear',
    scaleColors: ['#b6d6ff', '#005ace'],
    selectedColor: 'red',
    selectedRegion: null,
    showTooltip: true,
    onRegionClick: function (element, code, region) {
        var codeValue = $('#Code').text();
        var countryCode = $('#Code').text();
        var data = null;
        data = { CountryCode: codeValue.toUpperCase() }
        console.log(data);
        var Capital = null;
        $.ajax({
            type: "POST",
            url: 'Default.aspx/GetCapital',
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (code) {
                Capital = code;
                $('#Capital').text(Capital);
            }
        });
        var ISOCode = $('#Code');
        ISOCode.text(code);
        var Country = $('#Country');
        Country.text(region);
    }
});

    });