jQuery - 非法调用

yod*_*oda 97 ajax jquery post

jQuery v1.7.2

我有这个功能在执行时给我以下错误:

Uncaught TypeError: Illegal invocation
Run Code Online (Sandbox Code Playgroud)

这是功能:

$('form[name="twp-tool-distance-form"]').on('submit', function(e) {
    e.preventDefault();

    var from = $('form[name="twp-tool-distance-form"] input[name="from"]');
    var to = $('form[name="twp-tool-distance-form"] input[name="to"]');
    var unit = $('form[name="twp-tool-distance-form"] input[name="unit"]');
    var speed = game.unit.speed($(unit).val());

    if (!/^\d{3}\|\d{3}$/.test($(from).val()))
    {
        $(from).css('border-color', 'red');
        return false;
    }

    if (!/^\d{3}\|\d{3}$/.test($(to).val()))
    {
        $(to).css('border-color', 'red');
        return false;
    }

    var data = {
        from : from,
        to : to,
        speed : speed
    };

    $.ajax({
        url : base_url+'index.php',
        type: 'POST',
        dataType: 'json',
        data: data,
        cache : false
    }).done(function(response) {
        alert(response);
    });

    return false;
});
Run Code Online (Sandbox Code Playgroud)

如果我data从ajax调用中删除它,它的工作..任何建议?

谢谢!

Les*_*sar 112

我认为您需要将字符串作为数据值.它可能是jQuery内部没有正确编码/序列化To&From对象的东西.

尝试:

var data = {
    from : from.val(),
    to : to.val(),
    speed : speed
};
Run Code Online (Sandbox Code Playgroud)

另请注意以下内容:

$(from).css(...
$(to).css(
Run Code Online (Sandbox Code Playgroud)

您不需要jQuery包装器,因为To&From已经是jQuery对象.

  • 这种方法对我有帮助.我将变量命名为`$ from = $('#from');`这有助于我记住它代表一个jQuery对象,它有助于避免在某个字符串上调用方法或尝试使用`.toString操作字符串( )`或者是jQuery对象的东西. (3认同)
  • 谢谢,忘了我加载了对象而不是字符串,通常我加载字符串:) (2认同)

小智 102

尝试在像这样的ajax设置中设置processData:false

$.ajax({
    url : base_url+'index.php',
    type: 'POST',
    dataType: 'json',
    data: data,
    cache : false,
    processData: false
}).done(function(response) {
    alert(response);
});
Run Code Online (Sandbox Code Playgroud)

  • `默认情况下,作为对象传入数据选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型"application/x-www-form-urlencoded" ".如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false (6认同)
  • 您可能还需要添加 `contentType: false` 。我上传文件时这样做了。请参阅/sf/ask/1473135891/ (2认同)

Iva*_*nić 18

只是为了记录,如果你试图在数据中使用未声明的变量,也会发生这种情况

var layout = {};
$.ajax({
  ...
  data: {
    layout: laoyut // notice misspelled variable name
  },
  ...
});
Run Code Online (Sandbox Code Playgroud)


Bab*_*med 10

如果您想使用Javascript FormData API 提交表单并上传文件,您需要设置以下两个选项:

processData: false,
contentType: false
Run Code Online (Sandbox Code Playgroud)

你可以尝试如下:

//Ajax Form Submission
$(document).on("click", ".afs", function (e) {
    e.preventDefault();
    e.stopPropagation();
    var thisBtn = $(this);
    var thisForm = thisBtn.closest("form");
    var formData = new FormData(thisForm[0]);
    //var formData = thisForm.serializeArray();

    $.ajax({
        type: "POST",
        url: "<?=base_url();?>assignment/createAssignment",
        data: formData,
        processData: false,
        contentType: false,
        success:function(data){
            if(data=='yes')
            {
                alert('Success! Record inserted successfully');
            }
            else if(data=='no')
            {
                alert('Error! Record not inserted successfully')
            }
            else
            {
                alert('Error! Try again');
            }
        }
    });
});
Run Code Online (Sandbox Code Playgroud)


hyg*_*ull 7

就我而言,我只是改变了

注意:这是在 Django 的情况下,所以我添加了csrftoken. 在您的情况下,您可能不需要它。

添加contentType: falseprocessData: false

注释掉 "Content-Type": "application/json"

$.ajax({
    url: location.pathname, 
    type: "POST",
    crossDomain: true,
    dataType: "json",
    headers: {
        "X-CSRFToken": csrftoken,
        "Content-Type": "application/json"
    },
    data:formData,
    success: (response, textStatus, jQxhr) => {

    },
    error: (jQxhr, textStatus, errorThrown) => {

    }
})
Run Code Online (Sandbox Code Playgroud)

$.ajax({
    url: location.pathname, 
    type: "POST",
    crossDomain: true,
    dataType: "json",
    contentType: false,
    processData: false,
    headers: {
        "X-CSRFToken": csrftoken
        // "Content-Type": "application/json",
    },
    data:formData,
    success: (response, textStatus, jQxhr) => {

    },
    error: (jQxhr, textStatus, errorThrown) => {

    }
})
Run Code Online (Sandbox Code Playgroud)

它奏效了。


小智 5

就我而言,我没有定义在 ajax 中传递给数据的所有变量。

var page = 1;

$.ajax({
    url: 'your_url',
    type: "post",
    data: { 'page' : page, 'search_candidate' : search_candidate }
    success: function(result){
        alert('function called');
    }
)}
Run Code Online (Sandbox Code Playgroud)

我刚刚定义了变量var search_candidate = "candidate name";及其工作原理。

var page = 1;
var search_candidate = "candidate name"; // defined
$.ajax({
    url: 'your_url',
    type: "post",
    data: { 'page' : page, 'search_candidate' : search_candidate }
    success: function(result){
        alert('function called');
    }
)}
Run Code Online (Sandbox Code Playgroud)