循环 webmethod,以便 2 个 ajax 调用检索 2 个不同的调用

本文关键字:调用 检索 webmethod 循环 ajax 以便 | 更新日期: 2023-09-27 18:31:27

我混合了很多信息,但它工作正常,直到我发现我想要 2 个甜甜圈图,然后我遇到了问题,我超出了界限,我试图找到一种方法来循环 webmethod,所以它接收 2 个 ajax 调用,它确实收到了它们,但它没有像我想要的那样在 webmethod 中循环, 所以它检索不同,就像 chart1 应该是关于船舶的东西,而 chart2 应该是关于其他东西的。

就像,它应该是一个循环,其中查询字符串正在更改,因此它将从查询中获得 2 个不同的 Select 命令。

网络方法背后的代码

[WebMethod]
public static string GetChart ( string chart )
{
    string constr1 = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
    using ( SqlConnection con = new SqlConnection ( constr1 ) )
    {
        string query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Chart1_ID = '{0}'", chart);
        string query2 = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Chart2_ID = '{0}'", chart);
        using ( SqlCommand cmd = new SqlCommand ( ) )
        {
            cmd.CommandText = query;
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open ( );
            using ( SqlDataReader sdr = cmd.ExecuteReader ( ) )
            {
                StringBuilder sb1 = new StringBuilder();
                sb1.Append ( "[" );
                while ( sdr.Read ( ) )
                {
                    sb1.Append ( "{" );
                    System.Threading.Thread.Sleep ( 50 );
                    sb1.Append ( string.Format ( "value:{0}, color: '{1}', highlight :'{2}', label :'{3}'" , sdr [ 0 ] , sdr [ 1 ] , sdr [ 2 ] , sdr [ 3 ] ) );
                    sb1.Append ( "}," );
                }
                sb1 = sb1.Remove ( sb1.Length - 1 , 1 );
                sb1.Append ( "]" );
                con.Close ( );            
                return sb1.ToString ( );
            }
        }
    }
}

jQuery

$( function ()
{
    $( '#<%= DropDownList_1.ClientID %>' ).bind( "change", function ()
    {
        LoadChart();
    } );
} );
function LoadChart()
{
    //setup an array of AJAX options, each object is an index that will specify information for a single AJAX request
    var ajaxes = [{ url: 'CS.aspx/GetChart', data: "{chart: '" + $( '#<%= DropDownList_1.ClientID %>' ).val() + "'}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart1' }, { url: 'CS.aspx/GetChart', data: "{motor: '" + $( '#<%= DropDownList_1.ClientID %>' ).val() + "'}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart2' }],
                    current = 0;
        //declare your function to run AJAX requests
        function loadCharts()
        {
            //check to make sure there are more requests to make
            if ( current < ajaxes.length )
            {
                //make the AJAX request with the given data from the `ajaxes` array of objects
                $.ajax( {
                type: "POST",
                url: ajaxes[current].url,
                data: ajaxes[current].data,
                contentType: ajaxes[current].contentType,
                dataType: ajaxes[current].dataType,
                success: function ( r )
                {
                    $( ajaxes[current].chart ).html( "" );
                    var data = eval( r.d );
                    var el = document.createElement( 'canvas' );
                    $( ajaxes[current].chart )[0].appendChild( el );
                    //Fix for IE 8
                    if ( $.browser.msie && $.browser.version === "8.0" )
                    {
                        G_vmlCanvasManager.initElement( el );
                    }
                    var ctx = el.getContext( '2d' );
                    var chartoptions =
                    {
                        animateScale: true,
                        animationEasing: "linear",
                        showTooltips: true,
                        animationSteps: 120
                    }
                    var userStrengthsChart = new Chart( ctx).Doughnut( data, chartoptions );
                    //increment the `current` counter and   recursively call this function again
                    current++;
                    loadCharts();
                    },
                    failure: function ( response )
                    {
                        alert( 'There was an error.' );
                    }
                } );
            }
        }
        //run the AJAX function for the first time once    `document.ready` fires
        loadCharts();
}

.ASPX 代码

<table>
    <tr>
        <td>Choose:
            <asp:DropDownList ID="DropDownList_1" runat="server">
            <asp:ListItem Text="Choose" Value="0" />
            <asp:ListItem Text="TestList" Value="1" />
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            <div id="dvChart1">
            </div>
        </td>
        <td>
            <div id="dvChart2">
            </div>
        </td>
    </tr>
</table>

就像顶部所说的那样,代码工作正常,在我想拥有 2 个图表之前,意味着..这只是网络方法,如果可能的话,我需要修复..循环它,所以它将循环 2 次,并有一个 arraylist 或类似的东西,所以 ajax 调用正在发送一个额外的数字,如"index:0"然后我检查"索引"是否为"0",然后做这个,或者类似的东西,我试图这样做。

我试图在"数据"中给它索引,然后通过以下方式接收它:

public static string GetChart (string chart, int index) 

然后它停止工作,当我尝试这样做时,我不确定我是否做对了,这就是为什么我提到它,如果有人知道如何这样做。我尝试这样做,就像使用 jQuery 代码一样,变量"array",以便我检查 if/else,"索引"是 0 或 1,因为我有 2 次调用。但它无法处理它,什么都没有真正发生。

WebMethod 背后的代码(包括索引)

[WebMethod]
    public static string GetChart ( string chart, int index )
    {      
        string constr1 = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        using ( SqlConnection con = new SqlConnection ( constr1 ) )
        {
            string query;
            if (index == 0)
            {
                query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
            }
            else
            {
                query = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
            }
            //string query = string.Format("SELECT Value1,Color1,Highlight1,Label1 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
            //string query2 = string.Format("SELECT Value2,Color2,Highlight2,Label2 FROM Tuning WHERE FK_Resultat_ID = '{0}'", chart);
            using ( SqlCommand cmd = new SqlCommand ( ) )
            {
                cmd.CommandText = query;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open ( );
                using ( SqlDataReader sdr = cmd.ExecuteReader ( ) )
                {
                    StringBuilder sb1 = new StringBuilder();
                    sb1.Append ( "[" );
                    while ( sdr.Read ( ) )
                    {
                        sb1.Append ( "{" );
                        System.Threading.Thread.Sleep ( 50 );
                        sb1.Append ( string.Format ( "value:{0}, color: '{1}', highlight :'{2}', label :'{3}'" , sdr [ 0 ] , sdr [ 1 ] , sdr [ 2 ] , sdr [ 3 ] ) );
                        sb1.Append ( "}," );
                    }
                    sb1 = sb1.Remove ( sb1.Length - 1 , 1 );
                    sb1.Append ( "]" );
                    con.Close ( );            
                    return sb1.ToString ( );
                }
            }
        }
    }

j查询(包括索引)

 var ajaxes = [{ url: 'CS.aspx/GetChart', data: "{chart: '" + $( '#<%= DropDownList_Motor.ClientID %>' ).val() + "'},{index:0}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart1' }, { url: 'CS.aspx/GetChart', data: "{motor: '" + $( '#<%= DropDownList_Motor.ClientID %>' ).val() + "'},{index:1}", contentType: "application/json; charset=utf-8", dataType: 'json', chart: '#dvChart2' }],
                    current = 0;

循环 webmethod,以便 2 个 ajax 调用检索 2 个不同的调用

我发现,我只需要将它们正确地分成 2 个单独的方法,我之前做错了。