我使用下面的脚本来处理我的登录表单并通过ajax发送登录请求,但是我收到错误。我以前从未经历过这个错误。代码如下:
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)
<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>
有人可以帮助我解决问题!
答案 0 :(得分:1)
您需要设置
processData 为false:默认情况下,作为对象传入数据选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型&#34; application / x-www-form-urlencoded&#34;。如果要发送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();
});
&#13;
<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>
&#13;