Invalid web service call, missing value for parame

2019-06-06 05:50发布

问题:

Is it possible for backbone to interface with asp.net soap webservice methods for saving and retrieving the data? because i got this error from the webmethod but actually the POST contains the parameters.

Server Side

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static Dummy SaveDummy(Dummy myDummy)
        {
            Dummy dumdum = myDummy;
            HttpContext.Current.Session["data"] = dumdum;
            return myDummy;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public static Dummy FetchDummy()
        {
            return (Dummy)HttpContext.Current.Session["data"];
        }

        public class Dummy
        {
            private string _name;
            private string _script;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public string Script
            {
                get
                {
                    return _script;
                }
                set { _script = value; }
            }
        }

Backbone Model

  window["model"] = Backbone.Model.extend({
        initialize: function () {
            console.log("CREATED");
        },
        defaults:{
            name:"Please enter your name",
            script:"Hello World"
        },
        urlRoot: "index.aspx/SaveDummy",
        validate: function (attrs) {

        }
    });

Application

$("document").ready(function () {

    var myModel = new model({
        name: "Stack Overflow",
        script: "alert('Hi SO')"
    });

    var myView = new view({
        model: myModel,
        el: $("#placeholder")
    });

    console.log("SAVING");
    myModel.save();

    console.log("FETCHING");
    myModel.fetch();

POST

{"name":"Stack Overflow","script":"alert('Hi SO')"}

Message

Invalid web service call, missing value for parameter: 'myDummy'.

Note

I did look into other posts with similar problem, which were solved by doing something like {myDummy={"name":"Stack Overflow","script":"alert('Hi SO')"}} . How could this be generated using Backbone?

回答1:

All of the server-side synchronization in Backbone is handled through Backbone.Sync which is designed for two things:

  • REST apis that work with JSON (not SOAP/XML)
  • Extensibility

So you will need to override the Backbone.Sync behavior to talk to your backend. It appears to be relatively straight-forward. Some guidance can be found in these links:

  • SO post about overriding Backbone.Sync
  • Blog post about consuming XML services in Backbone