【问题标题】:fetch value from JQuery to ASP.NET从 JQuery 获取值到 ASP.NET
【发布时间】:2013-11-06 16:48:03
【问题描述】:

我正在将低于“100 MYR”的值转换为不同国家的货币。转换器使用 JQuery (Google API)。我想将值(转换后的货币)传递给下面另一个页面中的标签(lblAmountPaid)。我尝试使用 session 和 cookies 方法但无法正常工作,它返回空字符串。请帮忙,谢谢。

ccGOOG.js

$(document).ready(function () {
$('#submit').click(function () {
    var errormsg = "";
    var amount = $('#txtAmount').val();
    var from = $('#drpFrom').val();
    var to = $('#drpTo').val();
    $.ajax({ type: "POST",
        url: "WebService.asmx/ConvertGOOG",
        data: "{amount:" + amount + ",fromCurrency:'" + from + "',toCurrency:'" + to + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () {
            $('#results').html("Converting...");
        },
        success: function (data) {
            $('#results').html(amount + ' ' + from + '=' + data.d.toFixed(2) + ' ' + to);
        },

        error: function (jqXHR, exception) {
            if (jqXHR.status === 0) {
                errormsg = 'Not connect.\n Verify Network.'; ;
            } else if (jqXHR.status == 404) {
                errormsg = 'Requested page not found. [404]'; ;
            } else if (jqXHR.status == 500) {
                errormsg = 'Internal Server Error [500].'; ;
            } else if (exception === 'parsererror') {
                errormsg = 'Requested JSON parse failed.'; ;
            } else if (exception === 'timeout') {
                errormsg = 'Time out error.'; ;
            } else if (exception === 'abort') {
                errormsg = 'Ajax request aborted.'; ;
            } else {
                errormsg = 'Uncaught Error.';
            }
            $('#results').html(errormsg);
            $('<a href="#" >Click here for more details</a>').click(function () {
                alert(jqXHR.responseText);
            }).appendTo('#results');
        }
    });
});
});

下面是另一个页面:

【问题讨论】:

    标签: c# javascript jquery asp.net session-cookies


    【解决方案1】:

    我建议您在 .ajax() 方法的 success 回调中调用 ASP.NET AJAX 页面方法,如下所示:

    首先,这里是启用Session的页面方法:

    [WebMethod(EnableSession = true)]
    public static void SetAmountInSession(int amount)
    {
        HttpContext.Current.Session["amount"] = amount;
    }
    

    接下来,您需要从 jQuery .ajax() 方法的 success 回调中调用它,并将 Google API 调用的结果传递给它,如下所示:

    success: function (data) {
        $('#results').html(amount + ' ' + from + '=' + data.d.toFixed(2) + ' ' + to);
        var args = {
            amount: data.d.toFixed(2)
        };
        $.ajax({
            type: "POST",
            url: "YourPage.aspx/SetSession",
            data: JSON.stringify(args),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function () {
                alert('Success.');
            },
            error: function () {
                alert("Fail");
            }
        });
    },
    

    最后,在你的“其他”页面,你可以获取Session中的值,像这样:

    // Check if value exists before we try to use it
    if(Session["amount"] != null)
    {
        lblTotalAmount.Text = Session["amount"].ToString();
    }
    

    【讨论】:

    • 感谢您的回复。我关注了所有内容,当我单击转换按钮时,它会显示一个消息框,上面写着“失败”,并且在另一页中没有返回值。我不明白为什么。
    猜你喜欢
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多