Backbone model.destroy不起作用,但是ajax delete call do

时间:2015-07-24 17:13:39

标签: javascript node.js backbone.js cors postman

我目前在使用主干的destroy方法时存在问题。

这是我的模特:

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,否则我的模型被视为新模型,甚至不会进行调用。

我的webapp在localhost上运行:63342

当我查看Chrome开发者工具中的网络标签时,我可以看到该呼叫已发送到此网址:

  

请求   网址: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();
    })
});

我在邮递员中尝试了相同的网址,它确实没有任何问题。知道为什么通过Backbone它不起作用?它会与任何CORS标题或类似内容相关吗?

由于

//编辑 来自网络标签的电话详情

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

1 个答案:

答案 0 :(得分:1)

I came across this other post: jQuery.ajax sending both OPTIONS and POST, how to handle with Express.js (Node.js) and it actually solved my problem.

My API was not answering correctly to the http options call made by my browser, so the DELETE call was never reaching my backend. The difference with Postman is that this option call is not made before the DELETE is send to the API. Now my backend respond with the proper headers to the options method and the DELETE call works exactly like in postman.

this is the code sample i added:

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();
}
相关问题