AJAX POST 请求错误 - `Uncaught TypeError: Illegal invocation`

时间:2021-01-03 06:15:12

标签: django ajax

如果我尝试这样做,我会收到此错误: if (old in sentence) == True:

Uncaught TypeError: Illegal invocation

如果我尝试这样做,我会收到此错误: $(document).on('input', '#search-inp', (e) => { $.ajax({ type: 'POST', url: '/search/', dataType: 'json', data: { input: $('#search-inp').val(), csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]'), }, success: function(data) { console.log(data); } }); });

403: Forbidden
$(document).on('input', '#search-inp', (e) => {
        $.ajax({
            type: 'POST',
            url: '/search/',
            dataType: 'json',
            processData: false,
            contentType: false,
            data: {
                input: $('#search-inp').val(),
                csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]'),
            },
            success: function(data) {
                console.log(data);
            }
        });
    });

可能是什么问题? 非常感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:1)

很抱歉打扰了大家。

$(document).on('input', '#search-inp', (e) => {
        $.ajax({
            type: 'POST',
            url: '/search/',
            dataType: 'json',
            data: {
                input: $('#search-inp').val(),
                csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
            },
            success: function(data) {
                console.log(data);
            }
        });
    });

我这样做了:csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]')

但应该这样做:csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()