2014-07-01 56 views
0

我有這個代碼從here。 頁面礦脈我喜歡送2 VAL「名」和「城市」如何在jQuery中發送數組並獲得在asp.net c#

這是claint側在服務器端Default.aspx.cs我希望得到的js代碼

$(document).ready(function() { 

$.post("Default.aspx", 
{ 
    name: "Donald Duck", 
    city: "Duckburg" 
}, 
function (data, status) { 
    alert("Data: " + data + "\nStatus: " + status); 
}); 

    }); 

2 VAL「名」和「城市」

這是服務端代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
{ 
    string id = Request.QueryString["name"]; 
    var status = Request.Form["city"]; 

    string fname = Request.Form["name"]; 
    string city = Request.Form["city"]; 


} 
} 

whay不工作???

+0

什麼是不工作?您是否嘗試過觀看瀏覽器開發工具的請求?您是否嘗試過在服務器上進行調試以查看這些變量值是什麼? –

+0

你需要在你的表單中創建一個web方法並調用它:http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ –

+0

Joe Enos在調試all val時null –

回答

0

在類中創建一個方法:

public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    [WebMethod] 
    public static string TestingAjax(string name,string city) 
    { 
    return name; 
    } 

} 

和使用jquery AJAX這樣稱呼它:

$(document).ready(function() { 

    var JsonData = JSON.stringify({ 
     name: "Donald Duck", 
     city: "Duckburg" 
});  
     $.ajax({ 
     type: "POST", 
     contentType: "application/json; charset=utf-8", 
     url: "Default.aspx/TestingAjax", 
     data: JsonData , 
     dataType: "json", 

     success: function(data) { 

     console.log(data); 

     }, 
     error: function(result) { 
     alert("Error"); 
     } 
     }); 
    }); 
+0

Ehsan Sajjad, 我將您的建議複製到我的視覺工作室。 它看起來他現在不這樣做了。 這裏scrae問題的副本 http://i.stack.imgur.com/xtIwr.png –

+0

嘗試使用[System.Web.Services.WebMethod]或包括System.Web.Services在您的文件(使用System.Web.Services;在你的文件頂部) – Jeroen1984

+0

@YehudaRaz你需要包括它的命名空間 –

相關問題