-2

How can I do with following questions?
1. Front-end site question is how to transfer Object-Array to JSON?
2. Back-end site question is how to get the JSON data from front-side?

Question 1:

how to make a data parameter in the AJAX?

 var vItems = [];
        var vItem = new Item('1', '11');
        vItems.push(vItem);
        vItem = new Item('2', '22');
        vItems.push(vItem);    

 function Item(Key,Val) {
        this.Key = Key;
        this.Val = Val;
    }

 $.ajax({
            type: "POST",
            url: ".............",
            data: ????????????????????  ,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert(response.d);  
            },
            failure: function (response) {
                alert(response.d);
            }
        });

Question 2:

How to make a correct parameter with C# function

 [System.Web.Services.WebMethod]
 public static string xxx(?????????)
 {    
        return "";
 }     
 public class VItems
 {
        public string Key;
        public string Val;
 }

PS: Please I've to use the same class between front-side and back-side like

Front-Side

         function Item(Key,Val) {
                     this.Key = Key;
                     this.Val = Val;
                 }

Back-Side

         public class VItems {
                     public string Key;
                     public string Val;
                 }

By the way, this issue isn't duplicative URL because it is just only one array-object, I need to handle multiple data

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
  • 1
    Possible duplicate of [Pass a user defined object to ASP.NET Webmethod from jQuery, using JSON](https://stackoverflow.com/questions/18078957/pass-a-user-defined-object-to-asp-net-webmethod-from-jquery-using-json) – Liam Aug 02 '18 at 10:03
  • @Liam I've seen it before,however it is just for one array object , i need to handle multiple Array-Objects – Willie Cheng Aug 02 '18 at 10:09
  • @Liam Thank very much for your help, I'm not sure how to send two arrays Json to server side (not correct JSON.stringify( vItems )) and how to get data from Client side (not correct public static string xxx(VItems Items) ) – Willie Cheng Aug 02 '18 at 10:20
  • @Liam Thank again,you have given me a lot of a great information, now I've tried and solved my problem, it is a little difference from your answer – Willie Cheng Aug 02 '18 at 10:29

1 Answers1

-1

I've solved my problem and hopefully it can help anyone who has the same problem

var vItems = [];
        var vItem = new Item('1', '11');
        vItems.push(vItem);
        vItem = new Item('2', '22');
        vItems.push(vItem);


$.ajax({
            type: "POST",
            url: "xxxxx.aspx/xxx",
            data: JSON.stringify({ Items: vItems } )  ,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
                success: function (response) {                        
            },
            failure: function (response) {
                alert(response.d);
            }
        });

      [WebMethod]
        public static string xxx(List<VItems> Items)
        {    
            return "";
        }

        public class VItems
        {
            public string Key;
            public string Val;
        }
Willie Cheng
  • 7,679
  • 13
  • 55
  • 68