2012-06-15 19 views
0

我寫過一個類,它使用jquerypost獲取發佈數據,然後執行一些crud操作。它工作正常添加的東西到數據庫中,但經過一定的數據時,它不重定向到頁面,下面的代碼:HttpContext.Current.Response.Redirect在類中的問題

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

    } 

爲什麼重定向犯規發生,還是我在錯誤做任何想法方式,我需要重定向到一個特定的頁面,一旦數據被注入數據庫。 任何幫助將不勝感激

回答

2

如果您使用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" 
      } 
     }); 
+0

我已經在我的問題中添加了jquery,所以你的意思是我應該返回d數據到jquery,然後執行if else語句。 –

+0

是的。正確... –

+0

如何檢查函數數據,我的意思是我如何調試jquery,我需要知道如果該方案正在傳遞給函數,因爲目前沒有任何事情發生 –

1
  • 回報selectedscheme或網址從您的網頁方法

  • 設置window.location基於jQuery的Web方法的結果

  • 需要設置WebMethod屬性爲您的C#方法

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

+0

如何我可以檢查函數數據,我的意思是我如何調試jquery,我需要知道是否該方案正在傳遞給函數,因爲目前沒有任何事情發生 –

+0

不知道爲什麼它返回[objectdocument]而不是schemetype –

+0

可能是jQuery無法識別內容類型文本或json等你需要給。 – Damith