【问题标题】:How to display information from Ajax POST request in aspx?如何在 aspx 中显示来自 Ajax POST 请求的信息?
【发布时间】:2019-03-23 10:58:09
【问题描述】:

我在 C# asp .net 页面中有一个简单的 js 代码,旨在获取签名并将其数据发送到数据库中。

var canvas = document.getElementById("signature");
        var w = window.innerWidth;
        var h = window.innerHeight;

        // Spec canvas dimensions
        canvas.width = 700;
        canvas.height = h / 2.5;

        var signaturePad = new SignaturePad(canvas, {
            dotSize: 1
        });

        document.getElementById("save").addEventListener("click", function (e) {

            if (!signaturePad.isEmpty()) {

                // Gets img data and split it to server
                var imageURI = signaturePad.toDataURL();
                var encoded_image = imageURI.split(',');
                var img_to_server = encoded_image[1];

                $.ajax({
                    url: "About.aspx/",
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    type: "POST",
                    dataType: "text",
                    data: { image: img_to_server },
                    success: function (msg) {
                        window.alert('success');
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        window.alert('no success');
                    }
                });
            }
        }, false);

该页面正确加载,我将 POST 请求发送到我的应用程序的另一个页面:About.aspx 在该页面上,我想获取发送的值以在查询中使用它,但我找不到将其存储在变量中的方法

    public partial class About : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Debug.WriteLine("Inside");
        string val = Request.Params["image"];
        Debug.WriteLine(val);
    }
}

我在这里错过了什么?

【问题讨论】:

  • XMLHttpRequest 是一个非常糟糕的参数名称......
  • 你不应该调用一个返回值的方法吗?我在任何地方都看不到任何回报。
  • 仅供参考 - 我不会使用网页来捕获和响应该 POST 请求。 WebAPI 控制器方法可能更合适。这样,它可以更轻松地使用 JSON 响应您的 ajax 调用,您可以更轻松地限制为 POST 等。
  • @xTwisteDx 即使我将其设为可返回的方法,我也没有正确返回它的 POST 数据..

标签: javascript c# asp.net .net ajax


【解决方案1】:

检索上传的文件:

HttpPostedFile file = Request.Files[0];

【讨论】:

    猜你喜欢
    • 2016-06-06
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多