ASP.NET Web Method that accepts a List is failing with “Web Service method name is not valid.”

后端 未结 10 798
感动是毒
感动是毒 2020-12-10 07:25

I want to create a web method that accepts a List of custom objects (passed in via jQuery/JSON).

When I run the website locally everything seems to work. jQuery an

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 08:03

    Alright, here's how it is for an object, just tested this: ============================================ASPX==================================

            $("#btngeMethodCallWithAnObjectAsAParameter").click(function (event) {
    
                $.ajax({
                    type: 'POST',
                    url: 'Default.aspx/PageMethodCallWithAnObjectAsAParameter',
                    data: '{"id":"' + '5' + '", "jSonAsset":' + JSON.stringify(new BuildJSonAsset()) + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d) {
                            alert(msg.d);
                        }
                    },
                    error: function () {
                        alert("Error! Try again...");
                    }
                });
    
            });
        });
    
    
        function BuildJSonAsset() {
            this.AssetId = '100';
            this.ContentId = '200';
            this.AssetName = 'Asset1';
        }
    

    ========================================================================

    [WebMethod()]
    public static string PageMethodCallWithAnObjectAsAParameter(int id, JSonAsset jSonAsset)
    {
        return "Success!";
    }
    

    ======================================================================

    And this works for a single object, next I am going to try a list of objects:)

提交回复
热议问题