C# 我可以通过C将结果返回到ajax吗#
我有一些代码将内部html传递给负责将其存储到数据库中的C#后面的代码。现在,我想将一些结果返回到ajax函数,它将帮助我在基于条件的基础上呈现页面。 下面是ajax函数和背后的代码 您可以在ajax的成功方法中看到我正在渲染到“list.aspx” 我希望在C#内部处理的条件下进行渲染 我想根据这个条件呈现页面 AjaxC# 我可以通过C将结果返回到ajax吗#,c#,jquery,ajax,C#,Jquery,Ajax,我有一些代码将内部html传递给负责将其存储到数据库中的C#后面的代码。现在,我想将一些结果返回到ajax函数,它将帮助我在基于条件的基础上呈现页面。 下面是ajax函数和背后的代码 您可以在ajax的成功方法中看到我正在渲染到“list.aspx” 我希望在C#内部处理的条件下进行渲染 我想根据这个条件呈现页面 Ajax $('#Button1').click(function () { var HTML = document.getElementB
$('#Button1').click(function () {
var HTML = document.getElementById("data").innerHTML;
var Fname = document.getElementById("MyText").value;
Senddata = { "HTML": HTML, "Fname": Fname };
$.ajax({
type: "Post",
url: "/wwwroot/Default.aspx/save",
data: JSON.stringify(Senddata),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
console.log(result);
// alert("Records Added");
window.open("List.aspx", "_self")
// window.location = result;
}
});
}
});
C#代码
[WebMethod(EnableSession=true)]
公共静态无效保存(字符串HTML、字符串Fname)
{
HttpContext.Current.Session["GUID"] = Convert.ToString(Guid.NewGuid());
try
{
............
{
con.Open();
}
string tempUser = Convert.ToString(HttpContext.Current.Session["UserID"]);
if (string.IsNullOrEmpty(tempUser))
{
tempUser = "0";
}
....................
.
con.Close();
if (tempUser == "0")
{
HttpContext.Current.Response.Redirect("login.aspx");
}
else
{
HttpContext.Current.Response.Redirect("list.aspx");
}
}
catch (Exception ex)
{
CommonBLL.WriteExceptionLog(ex, "Form Save Default.aspx");
throw ex;
}
}
我的理解是,您希望将数据从Save方法发送回Ajax成功回调…我是否正确?是的,绝对正确@VimIs Save工作正常?它工作正常,但在检查条件if(tempUser==“0”)后{//它需要返回一些结果,帮助ajax的成功方法呈现login.aspx HttpContext.Current.Response.Redirect(“login.aspx”);它从不呈现或将我带到login.aspx页面。将Save方法的返回类型更改为string,而不是void。示例:public static string Save(string HTML,string Fname),然后在con.Close()之后,返回一个字符串值,并确保在成功回调时收到。如果需要代码示例,请告知我们。
This is how your c# code should look like. Let's discuss in chat if you have more questions.
//change the return type to string
[WebMethod(EnableSession = true)]
public static string save(string HTML, string Fname)
{
....
try
{
....
if (string.IsNullOrEmpty(tempUser))
{
tempUser = "0";
return "some value 1";
}
....
con.Close();
return "some value 2";
}
catch (Exception ex)
{
CommonBLL.WriteExceptionLog(ex, "Form Save Default.aspx");
throw ex;
}
}