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

后端 未结 10 764
感动是毒
感动是毒 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 07:56

    Have to say, I am not 100% sold my answer is (all) the issue, BUT this would address the deserialization. Adding some detail for your specific object.

    on client side:

    function itemObject (objectTitle,  objectDescription ,objectValue )
    {
        this.ObjectTitle  = objectTitle,;
        this.ObjectDescription  = objectDescription ;
        this.ObjectValue  = objectValue ;
    }; 
    
    //  4 objects in source-fix for your list
    function objRow()
    {
        this.myCode = myCode;
        this.myCodeText = myCodeText;
        this.customObjectList  = new Array();
        for (k = 0; k < 4; k++)
        {
            this.customObjectList [k] = new itemObject (sourceTitle[k],sourceDescription[k],sourceValue[k]);
        };
    };
    var myCode = "hicode1";
    var  myCodeText = "hicodeText1";
    $(function()
    {
       function SaveCurrentObjects()
        {
             var currentSet = new objRow();
            var objectData = "";
            var testData = {objectSaveData : currentSet };
            objectData = JSON.stringify(testData);
    
            SaveObjectsData(objectData );
        };
    
        /* save the objects */
        function SaveObjectsData(objectSaveData)
        {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: objectSaveData,
                dataFilter: function(data)
                {
                    var msg;
                    if (typeof (JSON) !== 'undefined' &&
                    typeof (JSON.parse) === 'function')
                        msg = JSON.parse(data);
                    else
                        msg = eval('(' + data + ')');
                    if (msg.hasOwnProperty('d'))
                        return msg.d;
                    else
                        return msg;
                },
                url: "/manageobjects.asmx/EditCustomObjects", 
                success: function(msg)
                {
                   //do stuff
                },
                failure: function(msg)
                {
                   //handlefail
                }
            });
        };
    
    });
    

    on server side you would have:(exact name of "objectSaveData") as in client code.

    [WebMethod]
    public static string EditCustomObjects(ObjectData objectSaveData)
    {
       // code to save your object
       return "saved objects";
    }
    

    create a ObjectData class cointaining a list of objectItem similar to my other example matching up the objects (careful of names).

    NOTE: I have the ASP.NET ajax extensions installed - working with asp.net 2.0 code for this example.

    0 讨论(0)
  • 2020-12-10 08:01

    Maybe the path you are passing with jquery does not exist try to see the response using FireBug.

    0 讨论(0)
  • 2020-12-10 08:02

    This is a wrong declaration in javascript.

    var itemObject = { 
      ObjectTitle = objectTitle,
      ObjectDescription = objectDescription,
      ObjectValue = objectValue
    }
    

    Correct object initialization should be:

    var itemObject = { 
      ObjectTitle : objectTitle,
      ObjectDescription : objectDescription,
      ObjectValue : objectValue
    };
    

    Also try temporary removing async: false from $.ajax's parameters.

    0 讨论(0)
  • 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:)

    0 讨论(0)
提交回复
热议问题