How To Pass a MultiDimensional Array from Javascript to server using PageMethods in ASP.Net

萝らか妹 提交于 2019-12-13 15:47:12

问题


I have some <li> Items in my HTML Page like this

<li id="A1" class="ui-state-default">Item 2</li>
  <li id="A2" class="ui-state-default">Item 3</li>
  <li id="A3" class="ui-state-default">Item 4</li>
  <li id="A4" class="ui-state-default">Item 5</li>

I called It using Javascript and Jquery like this to get the Id and Index of the <li> elements

function LiOrder() {
            var order = $('li').map(function (i) {
                return { id: this.id, index: i };
            }).get();

            PageMethods.GetServerResponse(order, OnSuccess, OnFail);
        }

        function OnSuccess(arg) {
            alert(arg);
        }

WebMethod Written in Aspx.cs Page is this

[WebMethod]
    public static string GetServerResponse(string[,] LiOrder)
    {


        return LiOrder[0,0];
    }

But when I try to execute it I get Error like this in Google Chrome (Javascript Console)

POST http://localhost:2453/ERP29.1.13/Production/LifeCycleRegistration.aspx/GetServerResponse 500 (Internal Server Error) ScriptResource.axd:6979

What might be the reason for this error. I could pass a string to WebMethod like this. But when it comes to Array, its like this. Please Help me to resolve this


回答1:


JSON.stringfy resolved my issue. I changed my code like this

var LiArr=JSON.stringify(order);
            PageMethods.GetServerResponse(LiArr, OnSuccess, OnFail);


来源:https://stackoverflow.com/questions/15291347/how-to-pass-a-multidimensional-array-from-javascript-to-server-using-pagemethods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!