【问题标题】:Value passed by ajax from Javascript to C#, Replied but not savedajax 将值从 Javascript 传递到 C#,已回复但未保存
【发布时间】:2019-03-29 17:09:02
【问题描述】:

我使用 ajax 将一个值传递给我的 C# 部分,然后我得到了响应。但我无法保存该值或在我的 C# 代码中使用它。更多信息如下:

Ajax 调用:(gallery.aspx)

    $.ajax({
        url: Url, //assigned previously
        data: 'call=ajax&method=GetList&typeIds=' + typeIds.replace(",",""),
            type: 'POST',
            dataType: 'json',
            success: function (resp) {
                console.log("From AJAX: " + resp) // this works and shows the result

            },
             error: function (xhr, status) {
                console.log("Sorry, there was a problem!");
            }
    });

后面的代码(CodeFile):(gallery.aspx.cs)

更新:完整的 C# 代码 sn-p

public partial class gallery : System.Web.UI.Page
{
        public List<string> images = new List<string>();    
        public List<string> imagesList = new List<string>();
        public List<string> filteredImagesList = new List<string>();
        public List<string> testList = new List<string>();
        protected string imagesName;
        protected string filterType;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["call"].ParseString() == "ajax")
            {
                Response.Write(Request["typeIds"].ParseString().TrimEnd(','), true, ResponseType.Json);
                filterType = Request["typeIds"].ParseString().TrimEnd(',');
            }
            else
            {
                filterType = "Not Assigned!";
            }
        }
}

页面上的输出:未分配! 含义 &lt;h1&gt;&lt;%=filterType%&gt;&lt;/h1&gt; 在 aspx 文件中返回 aspx.cs 文件中的 else 语句

但是当console.log("From AJAX: " + resp) 显示结果时,我在我的javascript 中得到了响应。

但是我不能在我的 c# 代码文件中使用 filtertype 的值。

我不明白Response.Write(Request["type"].ParseString().TrimEnd(','), true, ResponseType.Json); 为何将 Request["type"] 返还给 js 部分,但不将其保存到我的代码文件中。应该是 Response.Read 或 Response.Save 之类的吗?

有人知道这里发生了什么吗?

【问题讨论】:

  • 请详细说明 C# 代码文件的含义。您是在谈论文件背后的代码还是在 Razor 页面中的 C# 行中谈论?只需使用您的 C# sn-p 来自的整个方法即可。
  • 是的,它背后的代码,但我总是使用 CodeFile="myfile.aspx.cs" 作为顶部的@标签
  • 您的 C# 代码是否包含在方法中,如果是,请您使用围绕您的 C# sn-p 的代码更新您的问题。
  • @Chris 我更新了我的问题。如果我应该分享更多信息,请告诉我
  • Output on the page: Not Assigned! 是什么意思。你什么时候看到这个结果?

标签: javascript c# jquery asp.net ajax


【解决方案1】:

您可以将 ajax 响应存储在隐藏字段中,然后在服务器端代码中访问该隐藏字段值。

<asp:HiddenField ID="HiddenField1" runat="server" />  

$.ajax({
        url: Url, //assigned previously
        data: 'call=ajax&method=GetList&type=' + typeIds.replace(",",""),
            type: 'POST',
            dataType: 'json',
            success: function (resp) {
                console.log("From AJAX: " + resp) // this works and shows the result
                 $('#<%=HiddenField1.CliendId%>').val(resp);

            },
             error: function (xhr, status) {
                console.log("Sorry, there was a problem!");
            }
    });

然后您可以像这样在服务器端代码中访问:

HiddenField1.Value

【讨论】:

  • 当我添加 asp:HiddenField 时项目崩溃
  • 你能详细说明项目崩溃是什么意思
  • 刷新时出现 301 错误。我遇到的项目一旦 301 错误命中它就会重定向并且不记录错误数据
  • 隐藏字段不应该与301重定向有任何关系
  • 我知道但奇怪的是我可以使用 asp:label 或其他 asp: 属性。但是当我用 asp:label 做同样的把戏时,它就不起作用了
【解决方案2】:

好的。它永远不会以您的方式分配。仅仅是因为当页面正在加载并且所有控件都在渲染时,没有考虑到 Page Life Cycles 的 ajax 调用。因为没有分配页面加载filterType。

虽然你有几个选择

  1. 使用带有更新面板的 Ajax 和 PageRequestManager class
  2. 通过 JQuery 或 Vanilla JS 使用 &lt;%=filterType.ClientID%&gt; 更改值

如果您只需要前端表示,您可以使用任一选项。如果您需要它用于进一步的后端,恐怕选项 1 是您的唯一选择。

【讨论】:

  • 但是响应如何正常工作。 Response.Write 工作正常,并将所需的响应写回 js
  • 因为它响应了 Ajax 请求,所以所有控件都已设置
猜你喜欢
  • 1970-01-01
  • 2019-11-24
  • 2015-09-19
  • 2014-10-21
  • 1970-01-01
  • 2014-01-17
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多