Backbone model.destroy 不起作用,但 ajax 删除调用

Backbone model.destroy doesn't work but ajax delete call do

我目前对 backbone 的销毁方法有疑问。

这是我的模型:

var FavoritePlace = Backbone.Model.extend({
    urlRoot: function() {
        return 'http://localhost:3000/api/1.0/users/' + this.userId + '/places';
    },
    initialize: function(userId) {
        this.userId = userId;
    }
});

在我看来,这是试图删除的函数:

var placeToRemove = userFavoritePlaces.get(place);
    placeToRemove = new FavoritePlace({id : place.attributes.placeid});
    placeToRemove.userId = user.attributes.id;
    placeToRemove.destroy({
        success: function() {
            self.isFavorite(null);
        }
    });
    userFavoritePlaces.remove(placeToRemove);

我用 id 属性创建了一个新的 FavoritePlace,否则我的模型被认为是新的,它甚至不会调用。

我的网络应用程序在 localhost:63342

上运行

当我查看 Chrome 开发人员工具中的网络选项卡时,我可以看到调用已发送至此 URL:

Request URL:http://localhost:3000/api/1.0/users/8/places/2

路由服务器端是这样的:

router.delete('/users/:user_id/places/:place_id', function(req, res, next) {
    dataQuery.userDeletePlaceFromFavorite(req, function(err, result) {
        if (err) {
            req.stats.error = err;
            res.status(err.httpCode).json({error: err.error});
        }
        else {
            res.json(result);
        }

        next();
    })
});

我在 postman 中尝试了相同的 url,它没有任何问题。知道为什么通过 Backbone 它不起作用吗?它会与任何 CORS headers 或类似的东西有关吗?

谢谢

// 已编辑 来自网络选项卡的呼叫详情

curl 'http://localhost:3000/api/1.0/users/8/places/2?apikey=2yIsVhfg' -X OPTIONS -H 'Access-Control-Request-Method: DELETE' -H 'Origin: http://localhost:63342' -H 'Referer: http://localhost:63342/cmweb/index.html' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36' -H 'Access-Control-Request-Headers: accept' --compressed

邮递员来电详情

    Access-Control-Allow-Headers → X-Requested-With, origin, content-type, accept
Access-Control-Allow-Method → GET, POST, DELETE
Access-Control-Allow-Origin → *
Connection → keep-alive
Content-Length → 21
Content-Type → application/json; charset=utf-8
Date → Fri, 24 Jul 2015 17:35:31 GMT
Vary → Accept-Encoding
X-Powered-By → Express

我遇到了另一个 post:jQuery.ajax sending both OPTIONS and POST, how to handle with Express.js (Node.js) 它实际上解决了我的问题。

我的 API 没有正确响应浏览器发出的 http 选项调用,因此 DELETE 调用从未到达我的后端。与 Postman 的不同之处在于,在将 DELETE 发送到 API 之前不会进行此选项调用。现在我的后端以正确的 headers 响应选项方法,并且 DELETE 调用的工作方式与 postman.

中的完全一样

这是我添加的代码示例:

if (req.method === 'OPTIONS') {
        console.log('!OPTIONS');
        var headers = {};
        // IE8 does not allow domains to be specified, just the *
        // headers["Access-Control-Allow-Origin"] = req.headers.origin;
        headers["Access-Control-Allow-Origin"] = "*";
        headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
        headers["Access-Control-Allow-Credentials"] = false;
        headers["Access-Control-Max-Age"] = '86400'; // 24 hours
        headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
        res.writeHead(200, headers);
        res.end();
}