0

I have written a class which gets the post data using jquerypost and then perform some crud operations. Its works fine adding stuff to the database , but it doesnt redirects to the page when passing certain data , below is the code :

 $('#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;
    }
    }

    }

Any ideas why the redirect doesnt takes place, or am i doing it in a wrong way , i need to redirect to a particular page once the data is injected to the db. Any assistance will be appreciated

Mr A
  • 6,448
  • 25
  • 83
  • 137

2 Answers2

2

If you are calling the POST method using AJAX, redirection at server side will not work.

You will have to redirect it at client side after request completion using javascript the request.

$('#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"
           }
       });
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
  • i have added the jquery in my question , so u mean i should return d data to jquery and then perform if else statement. – Mr A Jun 15 '12 at 10:23
  • how can i check the function data , i mean how can i debug the jquery , i need to know if the scheme is being passed to the function, coz currently nothing is happening – Mr A Jun 15 '12 at 10:28
  • You have to return selectedscheme from the server side and have to match the value in your javascript function function (data){} – Asif Mushtaq Jun 15 '12 at 10:31
  • still it doesnt redirects , i have debugged the class , its defo returning the scheme type , but i guess there is an issue with the function(data) – Mr A Jun 15 '12 at 10:42
  • put `alert(data);` and check what you get – Damith Jun 15 '12 at 10:44
1
  • return selectedscheme or URL from your page method

  • set window.location based on web method result on jquery

  • need to set WebMethod attribute for your C# method

Check Using jQuery to Call ASP.NET AJAX Page Methods – By Example

Damith
  • 62,401
  • 13
  • 102
  • 153
  • how can i check the function data , i mean how can i debug the jquery , i need to know if the scheme is being passed to the function, coz currently nothing is happening – Mr A Jun 15 '12 at 10:30
  • dunno why its returning [objectdocument] instead of schemetype – Mr A Jun 15 '12 at 11:05
  • May be jQuery can't identify content type text or json etc. you need to give that. – Damith Jun 15 '12 at 11:08
  • how to do that ? as i am not using json – Mr A Jun 15 '12 at 11:11