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对象.
小智 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)
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)
就我而言,我只是改变了
注意:这是在 Django 的情况下,所以我添加了csrftoken. 在您的情况下,您可能不需要它。
添加
contentType: false,processData: 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)
| 归档时间: |
|
| 查看次数: |
261896 次 |
| 最近记录: |