totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

Delete method with JQuery #576

Closed dacrhu closed 6 years ago

dacrhu commented 6 years ago

Hi All,

I try send DELETE method with JQuery. If I use F.restful2 then working fine, but if I use F.route then I get 404.

F.route('/api/auth', logout, ['delete', '*Auth', 'authorize', 'cors']);

I do not need restful always.

Response headers:

Access-Control-Allow-Headers:* Access-Control-Allow-Methods:POST Access-Control-Allow-Origin:http://localhost:8000 Access-Control-Max-Age:120 Connection:keep-alive Date:Sat, 11 Nov 2017 17:53:09 GMT Transfer-Encoding:chunked

Can you help me? Thank you, David

dacrhu commented 6 years ago

Note: If I call F.route with curl then it working fine.

petersirka commented 6 years ago

Hi @dacrhu, I have tested it and everything works. Can you test latest beta version $ npm install total.js@beta? Or do you have a live URL for testing?

Can I see your client-side source-code?

dacrhu commented 6 years ago

Result is same with beta.

Client side code:

$('#menu-logout').on('click', function(event) {
    $.ajax({
        url: config.protocoll + '://' + config.backendURL + '/api/auth',
        type: 'DELETE',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({
            "id": id
        }),
       headers: {
            'Authorization': 'Bearer ' + localStorage.getItem(config.tokenName),
        }
    }).done(function(data) {
        window.location.href = config.protocoll + '://' + config.backendURL + "/index.html";
    });
});

This is working with restful2:

     $.ajax({
            url: config.protocoll + '://' + config.backendURL + '/api/admin/users',
            type: 'delete',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({
                "id": id
            }),
            headers: {
                'Authorization': 'Bearer ' + localStorage.getItem(config.tokenName),
            },
            statusCode: {
                401: function() {
                    $('#menu-main').hide();
                    $('#content').load('/elements/login.html');
                }
            }
        }).done(function(data) {
            getusers(currentPage);
            $('#userremovewarning').modal('hide');
        });
petersirka commented 6 years ago

Can I see it via TeamViewer? Can you write me on Total.js messenger? It will be faster --> https://messenger.totaljs.com ... I have tested your code and works correctly. Maybe the problem will be in the schema, but I need to see your source-code.

molda commented 6 years ago

Aren't you missing 'json' flag in your route?

petersirka commented 6 years ago

Omg, I'm blind. @molda you're right.

dacrhu commented 6 years ago

I removed the schema because not need it.

This is the full code:

exports.install = function() {
    F.route('/api/auth', logout, ['delete', 'authorize', 'cors']);
}
function logout() {
    var self = this;
    SOURCE('redis').remove(CONFIG('redisDBSession'), self.user.id);
    SOURCE('redis').remove(CONFIG('redisDBChangePassword'), self.user.id);
    self.json(SUCCESS(true));
}

It is very simple.

@molda result is same with json flag.

Node.js : v6.11.4 Total.js : v2.8.0 OS : linux 4.13.5-200.fc26.x86_64

I use JQuery 3.2.1

petersirka commented 6 years ago

Then I need to test it in this case.

molda commented 6 years ago

So the json flag does not work?? F.route('/api/auth', logout, ['delete', 'authorize', 'cors', 'json']); <-- ???

dacrhu commented 6 years ago

@molda Unfortunately not.

petersirka commented 6 years ago

@dacrhu I need to test it, do you have TeamViewer or Google Remote Desktop? Write me email or write me on Total.js messenger.