我如何在ajax中构造一个字符串数组并将其传递给asp?

时间:2016-11-04 03:51:53

标签: javascript c# asp.net ajax

在我的项目中,我想使用ajax来实现JS和ASP之间的通信。但是,我收到500错误。

JS ajax中的JSON字符串:

 {lists: [{'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a111', 'colour': 'red'}, {'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a222', 'colour': 'green'}]}

C#:

 [WebMethod]
 public static string GetCurrentTime(List<string> lists)
 {
     return "text string" ;
 }

完整的AJAX:

var dataJson = "{lists: [{'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a111', 'colour': 'red'}, {'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a222', 'colour': 'green'}]}";
     $.ajax({
            type: "POST",
            url: URL,
            data: dataJson ,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
function OnSuccess(response) {
        console.log(response.d);
    }

2 个答案:

答案 0 :(得分:1)

尝试使用JSON.stringify,如下所示,而不是List<string>

var dataJson = {lists: [{'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a111', 'colour': 'red'}, {'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a222', 'colour': 'green'}]};
     $.ajax({
            type: "POST",
            url: URL,
            data: JSON.stringify(dataJson),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
function OnSuccess(response) {
        console.log(response.d);
    }

C#

public class Data { 
        public string ID {get;set;}
         public string colour {get;set;}   // This can be renamed into Pascal case here and in the json
       }

[WebMethod]
 public static string GetCurrentTime(List<Data> lists)
 {
     return "text string" ;
 }

答案 1 :(得分:0)

试试这个

<script>

        $(function () {
            //alert('jQuery');
            var URL = 'WebForm1.aspx/GetCurrentTime';
            var dataJson = { lists: [{ 'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a111', 'colour': 'red' }, { 'ID': 'a95fed0d-c1e4-45e8-2cd2-e5d7f099a222', 'colour': 'green'}] };
            $.ajax({
                type: "POST",
                url: URL,
                data: JSON.stringify(dataJson),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                }
            });
            function OnSuccess(response) {
                console.log(response.d);
            }
        });
    </script>

的.cs

public class ListObject
    {
        public string ID { get; set; }
        public string colour { get; set; }
    }
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetCurrentTime(object lists)
        {

            List<ListObject> objects = new List<ListObject>();
            Object[] results = (Object[])lists;

            foreach (Dictionary<String, Object> result in results)
            {
                var properties = result as Dictionary<string, object>;
                List<String> propertyNames = new List<string>();
                String ID=String.Empty;
                String colour = String.Empty;
                int idFlag = 0;
                foreach (var p in properties)
                {
                    if (idFlag == 0)
                    {
                        ID= p.Value.ToString();
                        idFlag = 1;
                    }
                    else
                    {
                        colour = p.Value.ToString();
                    }
                    //String sjs = p.ToString().Replace("[","").Replace("]","").Split(',')[1];

                }

                     objects.Add(new ListObject
                {
                    ID = ID,
                    colour = colour
                });
            }


            return "text string";
        }
    }