Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将HTTP基本身份验证标头添加到Backbone.js Sync函数可防止在Save()时更新模型_Javascript_Backbone.js_Http Headers_Cherrypy_Basic Authentication - Fatal编程技术网

Javascript 将HTTP基本身份验证标头添加到Backbone.js Sync函数可防止在Save()时更新模型

Javascript 将HTTP基本身份验证标头添加到Backbone.js Sync函数可防止在Save()时更新模型,javascript,backbone.js,http-headers,cherrypy,basic-authentication,Javascript,Backbone.js,Http Headers,Cherrypy,Basic Authentication,我正在开发一个web应用程序,它由使用Python的CherryPy框架编写的restful API提供支持。我开始使用jQuery和服务器端模板的组合编写用户界面,但最终切换到Backbone.js,因为jQuery已经失控了 不幸的是,我在让我的模型与服务器同步时遇到了一些问题。下面是我的代码中的一个快速示例: $(function() { var User = Backbone.Model.extend({ defaults: { id: n

我正在开发一个web应用程序,它由使用Python的CherryPy框架编写的restful API提供支持。我开始使用jQuery和服务器端模板的组合编写用户界面,但最终切换到Backbone.js,因为jQuery已经失控了

不幸的是,我在让我的模型与服务器同步时遇到了一些问题。下面是我的代码中的一个快速示例:

$(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请求。以下是相关位:

请求:

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}
var params = {type: type, dataType: 'json'};
var params = {
    type: type,
    dataType: 'json',
    headers: {
        'Authorization': getAuthHash()
    }
};
响应:

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}
var params = {type: type, dataType: 'json'};
var params = {
    type: type,
    dataType: 'json',
    headers: {
        'Authorization': getAuthHash()
    }
};
如您所见,服务器成功地处理了请求,并返回了
id
和创建的
的值。但是由于某种原因,当我的代码调用
console.log(u.id)时
,我得到
null
,当我的代码调用
console.log(u.created)时,我得到未定义的

tl;dr:为什么在调用
save()
后,Backbone.js不保存对我的对象的更改

编辑: 我修改了上述代码,以便在
success
回调中使用get函数访问模型属性。这将解决原始代码的任何并发问题

我还在模型的
parse
函数中添加了一些控制台日志记录。奇怪的是,每一个都是未定义的。。。这是否意味着Backbone.js无法解析我的响应JSON

编辑2: 几天前,我发现这个问题实际上是一个自定义头,我将它添加到每个请求中以启用HTTP基本身份验证。有关详细信息,请参阅。

此代码:

u.save();
console.log(u.id);
console.log(u.username);
console.log(u.token);
console.log(u.created);
立即运行。。。在这之后,没有任何东西可以运行,排队的ajax请求开始。然后,响应会晚一点出现,只有在这一点上,值才会发生更改


这些属性似乎并不直接在对象上,但是保存的异步处理仍然有效,即使您将代码更正为具有
console.log(u.get(“id”))
等,您也不会得到预期的结果。

我发现了这个问题,尽管我仍然无法解释为什么它是一个问题。我正在构建的web应用程序有一个身份验证过程,需要在所有请求中传递HTTP基本身份验证头

为了使主干网能够正常工作,我重写了Backbone.sync函数并更改为添加标头

原始代码:

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}
var params = {type: type, dataType: 'json'};
var params = {
    type: type,
    dataType: 'json',
    headers: {
        'Authorization': getAuthHash()
    }
};
修改代码:

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}
var params = {type: type, dataType: 'json'};
var params = {
    type: type,
    dataType: 'json',
    headers: {
        'Authorization': getAuthHash()
    }
};
函数getAuthHash()只返回一个Base64字符串,该字符串表示适当的身份验证信息

由于某些原因,添加此标头会导致同步/保存功能失败。如果我把它拿出来,一切都会按照你的预期进行

悬赏金仍然悬而未决,我会很高兴地奖励任何能够解释原因并提供问题解决方案的人

编辑:

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}
var params = {type: type, dataType: 'json'};
var params = {
    type: type,
    dataType: 'json',
    headers: {
        'Authorization': getAuthHash()
    }
};

看起来问题在于我将头添加到请求的方式。有一个很好的小JavaScript库,它通过正确添加HTTP Basic Auth头来解决这个问题。

尝试向ajax请求添加一个自定义beforeSend方法来添加自定义头

例如:

我已经测试了你的代码,它对我来说很好


请参见此处的演示,
jsfiddle.net/yxkUD/

您建议使用u.get(“id”)而不是u.id非常有意义,但即使我尝试在成功回调或模型的同步事件中记录这些属性,它们仍然未设置,所以我认为问题还有很多。我已经编辑了原始问题中的代码,以纳入您的建议。。。它仍然不起作用,我束手无策。@MusikPolice如果您已经定义了解析函数,那么您就有责任从响应返回解析后的模型。当来自服务器的响应不是直接构造为创建模型时,使用此函数from@MusikPolice您可以在解析函数中调试它,比如so
console.log(JSON.parse(JSON.stringify(response))
您的代码看起来很好(除了您应该使用
Model.urlRoot
而不是
Model.url
,但这不是问题所在)。早期版本的代码有问题,但您绝对确定当前编辑中的确切代码仍然失败吗?抱歉,我没有在问题中添加有关问题的其他详细信息。有关更多详细信息,请参阅