我写了一个类,它使用jquerypost获取帖子数据,然后执行一些crud操作。它的工作原理是向数据库添加东西,但是在传递某些数据时它不会重定向到页面,下面是代码:
$('#clickme').click(function (e) {
e.preventDefault();
indx = $("#DropDownList1 option:selected").index();
indx += 1;
var test = $("#DropDownList" + (indx + 1));
var url = "/Base/performOperations/shootme/"
jQuery.post(url, { name: jQuery("#name").val(), email: jQuery("#email").val(), federation: jQuery(test).val(), selectedscheme: jQuery("#DropDownList1").val() },
function (data) {
if (data == scheme1) {
window.location = "http://www.openme.com"
}
});
});
namespace pw
{
public class performOperations
{
public static string ShootMe() {
HttpRequest post = HttpContext.Current.Request;
string name = post["name"];
string email = post["email"];
string selectedscheme = post["selectedscheme"];
string federation = post["federation"];
string conn = System.Configuration.ConfigurationManager.AppSettings["mydb"];
string sql = "INSERT INTO dbo.mydb(Email,Name,schemes,LoginDate,schemeType) VALUES(@email,@name,@scheme,@dateTime,@federation)";
SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql,
new SqlParameter("@email", email),
new SqlParameter("@name", name),
new SqlParameter("@scheme", selectedscheme),
new SqlParameter("@dateTime", DateTime.Now),
new SqlParameter("@federation", federation));
return selectedscheme;
}
}
}
任何想法为什么没有发生重定向,或者我以错误的方式这样做,一旦数据注入数据库,我需要重定向到特定页面。任何帮助将不胜感激
如果使用 AJAX
调用 POST
方法,则服务器端的重定向将不起作用。
在请求完成后,您必须使用javascript
请求将其重定向到客户端。
$('#clickme').click(function (e) {
e.preventDefault();
indx = $("#DropDownList1 option:selected").index();
indx += 1;
var test = $("#DropDownList" + (indx + 1));
var url = "/Base/sample/Hello/"
jQuery.post(url, { name: jQuery("#name").val(), email: jQuery("#email").val(), federation: jQuery(test).val(), selectedscheme: jQuery("#DropDownList1").val() },
function (data) {
if (data == "shoothim") {
window.location = "http://www.cuthishead.com"
}
else if(data == "shoother")
{
window.location = "http://www.chopherbody.com"
}
else if(data == "shootboth")
{
window.location = "http://www.fulldeath.tv"
}
});
-
从页面方法返回
selectedscheme
或网址 -
基于 jquery 上的 Web 方法结果设置
window.location
-
需要为 C# 方法设置
WebMethod
属性
检查使用 jQuery 调用 ASP.NET AJAX 页面方法 – 通过示例