【发布时间】: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!";
}
}
}
页面上的输出:未分配!
含义 <h1><%=filterType%></h1> 在 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