【问题标题】:Calling a Page Method using jQuery - Backbone.Js now使用 jQuery 调用页面方法 - Backbone.Js 现在
【发布时间】:2012-02-07 04:32:14
【问题描述】:

使用下面的 javascript 代码,我正在尝试一些 BackboneJs 概念。无法弄清楚为什么调用 XHR 请求后的响应是整页的 HTML,而不是 Person class 的序列化版本。看看下面 服务器端代码为C# 和ASP.NET 2.0

注意:忘记模型上的url和urlroot,我使用的是backbonejs Sync

Javascript

window.Person = Backbone.Model.extend({
    defaults: {
        id: Math.random(),
        name: "Type your name"
    },
    initialize: function (model) {
        this.bind("change", this.ModelChanged);
    },
    ModelChanged: function () {

    },
    url: "CreatePerson",
    urlRoot: "/index.aspx/"
});

Backbone.sync = function (met, mod, op) {
    switch (met) {
        case "create":
            break;
        case "update":
            break;
        case "delete":
            break;
        case "read":
            break;
        default:
            break;

    }
};

服务器端代码

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static Person CreatePerson(Person newPerson)
    {
        List<Person> peopleList = HttpContext.Current.Session["People"] as List<Person>;
        if (peopleList == null)
        {
            peopleList = new List<Person>();
        }
        Person p1 = new Person();
        p1 = newPerson;
        peopleList.Add(p1);
        HttpContext.Current.Session["People"] = peopleList;
        return p1;
    }

人物类

public class Person
{

    public string Id
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }


}

终于有测试代码了

var x = new Person({
    name: "StackOverflow"
});
$.post("index.aspx/CreatePerson", "{" + JSON.stringify(x) + "}", function () {
    console.log(arguments)
});

【问题讨论】:

    标签: javascript jquery model-view-controller backbone.js underscore.js


    【解决方案1】:

    尝试设置contentType

    $.ajaxSetup({
    contentType: "application/json; charset=utf-8"
    });
    

    之后你的请求

    var x = new Person({
        name: "StackOverflow"
    });
    $.post("index.aspx/CreatePerson", "{" + JSON.stringify(x) + "}", function () {
        console.log(arguments)
    });
    

    【讨论】:

      【解决方案2】:

      我不明白为什么,但是 jQuery 让我按照这个奇怪的过程来完成这项工作。

      它将有效的 json 序列化为 URL 编码格式而不是错误的 json 字符串。

      $.ajaxSetup({
          contentType: "application/json; charset=utf-8"
      });
      
      Backbone.sync = function (met, mod, op) {
          console.log("method", met, "model", mod);
          switch (met) {
              case "create":
                  $.post("index.aspx/CreatePerson", { "newPerson": mod.attributes }, function () { console.log(arguments) }, "json");
                  break;
              case "update":
                  var x = { "newPerson": mod.attributes };                
                  x = JSON.stringify(x);                
                  $.post("index.aspx/CreatePerson", x, function () { console.log(arguments) });
                  break;
              case "delete":
                  break;
              case "read":
                  break;
              default:
                  break;
          }
      };
      

      【讨论】:

      • 您是否查看了 fiddler 2 的请求/响应日志记录?这会告诉你到底发生了什么
      • @GarethElms 嗨,是的,我确实在萤火虫帖子的详细信息中看过它们。不同之处在于 jquery 发送一个 HTTP POST 并且 URL 对参数进行编码,即帖子的数据,因此 key=value&amp;key=value 是帖子数据,但 asp.net webmethods 期望一个可以反序列化为最终对象的有效 JSON 字符串。因此,我不得不遵循这个奇怪但可行的程序。但是我需要指出何时通过脚本管理器生成代理我可以做到这一点并且它可以工作PageMethods.CreatePerson(model)
      猜你喜欢
      • 2010-10-09
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多