简体   繁体   English

如何在jquery中发送数组并在asp.net中获取C#

[英]how to send array in jquery and get in asp.net c#

i have this code from here . 我从这里有此代码。 in the page lode i like to send 2 val "name" and "city" 在页面lode中,我想发送2个val“名称”和“ city”

this is the claint side the js code 这是js程式的反面

$(document).ready(function () {

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

  });

in the server side Default.aspx.cs i like to get the 2 val "name" and "city" 在服务器端Default.aspx.cs中,我想获取2个值“名称”和“城市”

this is the serve side code 这是服务端代码

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 is not working??? 为何不起作用???

Create a method in your class: 在您的课程中创建一个方法:

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;
   }

}

and call it using jquery ajax like this: 并使用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");
        }
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM