Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4297 Lượt xem

My problem is that I cannot submit data to my controller from JS widget because it is "missing" a csfr token. But I've tried having a hidden field for the token and getting it from web.core. Nothing works!


Error in log:

 odoo URLs are CSRF-protected by default (when accessed with unsafe HTTP methods).


JS:

var formData = new FormData();
form.find('input').each(function() {
var el = $(this);
formData.append(el.attr('name'), el.val())

});
formData.append('csrf_token', core.csrf_token);
console.log(Object.fromEntries(formData));
$.ajax({
url: '/some-url/add',
data: formData,
processData: false,
contentType: false,
type: 'POST',
success: function (data) {
console.log('Submission was successful.');
console.log(data);
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
},
});


Py:

@http.route(['/some-url/add'], type='http', auth="public", method=['POST'])

What I'm doing wrong?


Ảnh đại diện
Huỷ bỏ
Tác giả

After changing controller type to json so it thrown the wrong type error and changing it back to http it magically started working!

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 5 23
3040
1
thg 5 23
5980
0
thg 7 21
4125
1
thg 10 15
10059
0
thg 5 23
3186