Backbone.js 发布 500 错误

发布于 2024-12-27 11:11:32 字数 2223 浏览 1 评论 0原文

有人知道如何将帖子的 ContentType 指定为 json/application 吗? 我以为我是,并且骨干默认情况下这样做,但从事实来看,它说它正在获取纯文本(请参阅评论) ,我想我需要找出另一种方法来指定它。

我正在使用 Backbone.js,并且尝试 POST 到不再是只读的 TastyPie API,当我尝试创建模型并 .save() 时收到 500 错误。这是我在此处找到的用于同步的代码片段: http://documentcloud.github.com/backbone/docs/backbone.html #section-124

   Backbone.sync = function(method, model, options){
        var type = methodMap[method];
        var params = _.extend({
        type: type,
        dataType: 'json'
        }, options);

        if (!params.url){
        params.url = getUrl(model) || urlError();
        }

        if (Backbone.emulateJSON){
        params.contentType = 'application/json';
        params.data = params.data ? {model: params.data} : {};
        }

        if (Backbone.emulateHTTP){
        if(type === 'PUT' || type === 'DELETE'){
           if (Backbone.emulateJSON) params.data._method = type;
           params.type = 'POST';
           params.beforeSend = function (xhr){
               xhr.setRequestHeader('X-HTTP-Method-Override', type);
            };
         }
         }

        if (params.type !== 'GET' && ! Backbone.emulateJSON){
        params.prorcessData = false;
        }

        return $.ajax(params);
        };





    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
            o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
        });
        return o;
    };

    $(function() {
        $('form').submit(function() {
        var dict = $('form').serializeObject();
        var new_task = new Backbone.Model({
        date: toString(dict.date),
        name: toString(dict.name),
        priority: toString(dict.priority)});
        console.log("new_task =" + new_task);
         new_task.save();
        console.log(dict);

        return false;
        });

    });


    });

Does anyone know how to specify the ContentType of the Post to json/application? I thought I was, and backbone did it by default, but judging by the fact it's saying it's getting plain text (see comments), I guess I need to figure out another way to specify it.

I am using Backbone.js and I am trying to POST to the TastyPie API that is no longer read only and I am receiving a 500 error when I try to make a model and .save() it. This is a code snippet I am using for my sync that i found here:
http://documentcloud.github.com/backbone/docs/backbone.html#section-124

   Backbone.sync = function(method, model, options){
        var type = methodMap[method];
        var params = _.extend({
        type: type,
        dataType: 'json'
        }, options);

        if (!params.url){
        params.url = getUrl(model) || urlError();
        }

        if (Backbone.emulateJSON){
        params.contentType = 'application/json';
        params.data = params.data ? {model: params.data} : {};
        }

        if (Backbone.emulateHTTP){
        if(type === 'PUT' || type === 'DELETE'){
           if (Backbone.emulateJSON) params.data._method = type;
           params.type = 'POST';
           params.beforeSend = function (xhr){
               xhr.setRequestHeader('X-HTTP-Method-Override', type);
            };
         }
         }

        if (params.type !== 'GET' && ! Backbone.emulateJSON){
        params.prorcessData = false;
        }

        return $.ajax(params);
        };





    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
            o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
        });
        return o;
    };

    $(function() {
        $('form').submit(function() {
        var dict = $('form').serializeObject();
        var new_task = new Backbone.Model({
        date: toString(dict.date),
        name: toString(dict.name),
        priority: toString(dict.priority)});
        console.log("new_task =" + new_task);
         new_task.save();
        console.log(dict);

        return false;
        });

    });


    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧街凉风 2025-01-03 11:11:32

尝试在代码中设置 Backbone.emulateJSON = true;

如果将此设置为 true,则会将 contentType 设置为“application/json”,这就是您要查找的内容。

您只需要设置此变量一次,因此一个好地方就在表单提交代码上方

$(function() {
    Backbone.emulateJSON = true;
    $('form').submit(function() {
        ...

Try setting Backbone.emulateJSON = true; in your code.

If this is set to true, then it sets the contentType to 'application/json' which is what you're looking for.

You only need to set this variable once, so a good place is right above your form submit code

$(function() {
    Backbone.emulateJSON = true;
    $('form').submit(function() {
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文