-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

4
  • 1
    Possible duplicate of Pass a user defined object to ASP.NET Webmethod from jQuery, using JSON Commented Aug 2, 2018 at 10:03
  • @Liam I've seen it before,however it is just for one array object , i need to handle multiple Array-Objects Commented Aug 2, 2018 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) ) Commented Aug 2, 2018 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 Commented Aug 2, 2018 at 10:29

1 Answer 1

-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;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

@Liam Thank again,you have given me a lot of a great information, now I've tried and solved my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.