0

我编写了一个类,它使用 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;
    }
    }

    }

任何为什么不发生重定向的想法,或者我是否以错误的方式进行操作,一旦数据注入数据库,我需要重定向到特定页面。任何帮助将不胜感激

4

2 回答 2

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"
           }
       });
于 2012-06-15T10:17:23.987 回答
1
  • 从您的页面方法返回selectedscheme或 URL

  • window.location根据 jquery 上的 web 方法结果设置

  • 需要WebMethod为您的 C# 方法设置属性

检查使用 jQuery 调用 ASP.NET AJAX 页面方法 - 通过示例

于 2012-06-15T10:23:56.667 回答