我编写了一个使用jquerypost获取post数据的类,然后执行一些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;
}
}
}
为什么重定向不会发生的任何想法,或者我是以错误的方式进行,我需要在数据注入数据库后重定向到特定页面。 任何帮助将不胜感激
答案 0 :(得分:2)
如果使用POST
调用AJAX
方法,则服务器端的重定向将无效。
使用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"
}
});
答案 1 :(得分:1)
从页面方法
selectedscheme
或网址
根据jquery上的Web方法结果设置window.location
需要为您的C#方法设置WebMethod
属性
检查Using jQuery to Call ASP.NET AJAX Page Methods – By Example