无效的Web服务调用,缺少Backbone和Webservice webmethods参数的值

时间:2011-10-06 05:56:09

标签: javascript web-services post backbone.js underscore.js

骨干网是否可以与asp.net soap webservice方法接口以保存和检索数据?因为我从web方法得到了这个错误,但实际上POST包含参数。

服务器端

[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) {

        }
    });

应用

$("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')"}

消息

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

注意

我确实调查了其他类似问题的帖子,这些帖子通过做类似的事情来解决 {myDummy={"name":"Stack Overflow","script":"alert('Hi SO')"}}。如何使用Backbone生成这个?

1 个答案:

答案 0 :(得分:2)

Backbone中的所有服务器端同步都是通过Backbone.Sync处理的,后者专为两件事而设计:

  • 使用JSON(不是SOAP / XML)的REST apis
  • 扩展

因此,您需要覆盖Backbone.Sync行为以与后端通信。它看起来相对简单。可以在以下链接中找到一些指导: