[英]Ajax error `Uncaught TypeError: Illegal invocation when making ajax call
I am using the script below to process my login form and send login request by ajax but I get the error. 我正在使用下面的脚本来处理我的登录表单并通过ajax发送登录请求,但出现错误。 I have never experienced this error before.
我以前从未遇到过此错误。 The code is here below:
代码如下:
Uncaught TypeError: Illegal invocation
at e (jquery.min.js:4)
at Ab (jquery.min.js:4)
at Function.r.param (jquery.min.js:4)
at Function.ajax (jquery.min.js:4)
at loginReq (admin:95)
at HTMLButtonElement.onclick (admin:15
2) 2)
<script language="javascript" type="text/javascript">
function loginReq() {
var username = $("#username").val();
var password = $("#password").val();
// Checking for blank fields.
if( username =='' || password ==''){
$('input[type="text"],input[type="password"]').css("border","2px solid red");
$('input[type="text"],input[type="password"]').css("box-shadow","0 0 3px red");
$('#login_alert').show();
$("#login_alert").fadeTo(2000, 500).slideUp(500, function(){
$("#login_alert").slideUp(500);
});
} else {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var link="login";
var formData = new FormData(document.getElementById('#login_form'));
$.ajax({
type: 'post',
dataType: 'html',
url: link,
cache: false,
data: formData,
success: function (result) {
var obj = jQuery.parseJSON(result);
var status = obj['success'];
if(status == 'success'){
window.location.href = "{{ url('dashboard') }}";
}else{
$("#login_alert2" ).show();
$("#login_alert2").fadeTo(2000, 500).slideUp(500, function(){
$("#login_alert2").slideUp(500);
});
}
}
});
}
}
</script>
Could someone help me what could be the issue! 有人可以帮我什么可能是问题!
You need to set 你需要设置
processData to false: By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".
processData为false:默认情况下,将处理作为对象传递给data选项的数据(从技术上讲,不是字符串),然后将其转换为查询字符串,以适合默认的内容类型“ application / x-www-窗体-urlencoded”。 If you want to send a DOMDocument, or other non-processed data, set this option to false.
如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false。
function loginReq() { var username = $("#username").val(); var password = $("#password").val(); // Checking for blank fields. if( username =='' || password ==''){ $('input[type="text"],input[type="password"]').css("border","2px solid red"); $('input[type="text"],input[type="password"]').css("box-shadow","0 0 3px red"); $('#login_alert').show(); $("#login_alert").fadeTo(2000, 500).slideUp(500, function(){ $("#login_alert").slideUp(500); }); } else { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); var link="login"; var formData = new FormData(document.getElementById('#login_form')); $.ajax({ type: 'post', dataType: 'html', url: link, cache: false, data: formData, processData: false, //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ success: function (result) { var obj = jQuery.parseJSON(result); var status = obj['success']; if(status == 'success'){ window.location.href = "{{ url('dashboard') }}"; }else{ $("#login_alert2" ).show(); $("#login_alert2").fadeTo(2000, 500).slideUp(500, function(){ $("#login_alert2").slideUp(500); }); } } }); } } $('#btn').on('click', function(e) { loginReq(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="login_form"> User name: <input type="text" id="username" value="username"> Password: <input type="password" id="password" value="pasword"> <button type="button" id="btn">Send Ajax</button> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.