从保存正在更新添加HTTP基本认证头到Backbone.js的同步功能prevents模型() [英] Adding HTTP Basic Authentication Header to Backbone.js Sync Function Prevents Model from Being Updated on Save()

查看:111
本文介绍了从保存正在更新添加HTTP基本认证头到Backbone.js的同步功能prevents模型()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作就是通过与Python的CherryPy的框架编写一个宁静的API驱动的Web应用程序。我开始了写作与jQuery和服务器端模板组合的用户界面,但最终切换到Backbone.js的,因为jQuery的是失控的。

I'm working on a web application that is powered by a restful API written with Python's CherryPy framework. I started out writing the user interface with a combination of jQuery and server side templates, but eventually switched to Backbone.js because the jQuery was getting out of hand.

不幸的是,我有让我的模型与服务器同步的一些问题。下面是从我的code一个简单的例子:

Unfortunately, I'm having some problems getting my models to sync with the server. Here's a quick example from my code:

$(function() {
    var User = Backbone.Model.extend({
        defaults: {
            id: null,
            username: null,
            token: null,
            token_expires: null,
            created: null
        },

        url: function() {
            return '/api/users';
        },

        parse: function(response, options) {
            console.log(response.id);
            console.log(response.username);
            console.log(response.token);
            console.log(response.created);
            return response;
        }
    });

    var u = new User();
    u.save({'username':'asdf', 'token':'asdf'}, {
        wait: true,
        success: function(model, response) {
            console.log(model.get('id'));
            console.log(model.get('username'));
            console.log(model.get('token'));
            console.log(model.get('created'));
        }
    });
});

正如你可能会说,这里的想法是要注册的服务的新用户。当我打电话 u.save(); ,骨干确实发送POST请求到服务器。以下是相关的位:

As you can probably tell, the idea here is to register a new user with the service. When I call u.save();, Backbone does indeed send a POST request to the server. Here are the relevant bits:

请求:

Request URL: http://localhost:8080/api/users
Request Method: POST
Request Body: {"username":"asdf","token":"asdf","id":null,"token_expires":null,"created":null}

响应:

Status Code: HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 109
Response Body: {"username": "asdf", "created": "2013-02-07T13:11:09.811507", "token": null, "id": 14, "token_expires": null}

如你所见,服务器成功处理请求并发送回一个 ID A值创建 。但由于某些原因,当我的code调用的console.log(u.id); ,我得到,当我的code调用的console.log(u.created); ,我得到未定义

As you can see, the server successfully processes the request and sends back an id and a value for created. But for some reason, when my code calls console.log(u.id);, I get null, and when my code calls console.log(u.created);, I get undefined.

TL;博士:为什么不是Backbone.js的通话后持续改变我的对象保存()

tl;dr: Why isn't Backbone.js persisting changes to my objects after a call to save()?

编辑:
我修改了上面的code,使模型属性是使用在成功回调get函数访问。这应该解决与原来的code的并发问题。

I've modified the above code so that the model properties are accessed using the get function in a success callback. This should solve any concurrency problems with the original code.

我还添加在模型中的解析函数一些控制台日志记录。奇怪的是,每一种是未定义 ...这是否意味着Backbone.js的失败来分析我的反应JSON?

I've also added some console logging in the model's parse function. Oddly enough, each of these is undefined... Does that mean that Backbone.js is failing to parse my response JSON?

编辑2:
前几天,我发现这个问题实际上是一个自定义标题,我是增加每个请求启用HTTP基本验证。请参见这个答案了解详情。

推荐答案

我想通了这个问题,但我仍然无法解释的为什么的这是一个问题,在所有。我正在创建的Web应用程序有一个需要HTTP基本认证头与所有请求通过一个验证过程。

I figured out the issue, although I'm still at a loss to explain why it's an issue at all. The web app that I'm building has an authentication process that requires an HTTP basic Authentication header to be passed with all requests.

在为了与骨干这项工作,我推翻了Backbone.sync功能,改变的行1398 添加标题。

In order to make this work with Backbone, I overrode the Backbone.sync function and changed line 1398 to add the header.

原来的code:

var params = {type: type, dataType: 'json'};

修改code:

var params = {
    type: type,
    dataType: 'json',
    headers: {
        'Authorization': getAuthHash()
    }
};

函数getAuthHash()只返回一个Base64字符串,再presents适当的身份验证信息。

The function getAuthHash() just returns a Base64 string that represents the appropriate authentication information.

由于某些原因,增加这个头使得同步/保存功能失效。如果我把它拿出来,一切正常,你可能期待它。

For some reason, the addition of this header makes the sync/save functions fail. If I take it out, everything works as you might expect it to.

赏金仍然是开放的,我会愉快地奖励给任何人谁可以解释这是为什么,以及提供解决问题的方法。

The bounty is still open, and I'll happily reward it to anybody who can explain why this is, as well as provide a solution to the problem.

编辑:

看起来问题是,我是加标题到请求的方式。有一个可爱的小JavaScript库 rel=\"nofollow\">,通过正确添加HTTP基本认证头解决了这个问题

It looks like the problem was the way that I was adding the header to the request. There's a nice little JavaScript library available on Github that solves this problem by correctly adding the HTTP Basic Auth header.

这篇关于从保存正在更新添加HTTP基本认证头到Backbone.js的同步功能prevents模型()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆