admin管理员组文章数量:1426491
Console states the following errors when I hit the submit button on my page:
process.js:19 Uncaught ReferenceError: data is not defined
at HTMLButtonElement.<anonymous> (process.js:19)
at HTMLButtonElement.dispatch (jquery-3.3.1.min.js:2)
at HTMLButtonElement.y.handle (jquery-3.3.1.min.js:2)
This is my jQuery AJAX code that executes when you hit the submit button. This is my first time playing with AJAX/jQuery - not sure what is happening so if someone could help me, that'd be greatly appreciated!
$(document).ready(function() {
$('#login_button').click(function()
{
event.preventDefault();
var formData = {
'email' : $('input[email=email').val(),
'password' : $('input[password=password').val()
};
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : formData,
dataType : 'json'
});
// using the done promise callback
if (data.success == false)
{
if(data.errors.email)
{
//toastr.error(''+data.errors.email+'', 'Oops!');
alert('Email error');
}
else if(data.errors.password)
{
//toastr.error(''+data.errors.password+'', 'Oops!');
alert('Password Error');
}
}
else
{
//toastr.success('Works!', 'WooHoo!');
alert('Works.');
}
});
});
Console states the following errors when I hit the submit button on my page:
process.js:19 Uncaught ReferenceError: data is not defined
at HTMLButtonElement.<anonymous> (process.js:19)
at HTMLButtonElement.dispatch (jquery-3.3.1.min.js:2)
at HTMLButtonElement.y.handle (jquery-3.3.1.min.js:2)
This is my jQuery AJAX code that executes when you hit the submit button. This is my first time playing with AJAX/jQuery - not sure what is happening so if someone could help me, that'd be greatly appreciated!
$(document).ready(function() {
$('#login_button').click(function()
{
event.preventDefault();
var formData = {
'email' : $('input[email=email').val(),
'password' : $('input[password=password').val()
};
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : formData,
dataType : 'json'
});
// using the done promise callback
if (data.success == false)
{
if(data.errors.email)
{
//toastr.error(''+data.errors.email+'', 'Oops!');
alert('Email error');
}
else if(data.errors.password)
{
//toastr.error(''+data.errors.password+'', 'Oops!');
alert('Password Error');
}
}
else
{
//toastr.success('Works!', 'WooHoo!');
alert('Works.');
}
});
});
Share
Improve this question
asked Jun 13, 2018 at 8:59
JitrenkaJitrenka
3051 gold badge2 silver badges10 bronze badges
3
-
4
The
data
object you use in theif
conditions is not declared anywhere. You need to either use thesuccess
handler function of$.ajax
, or use adone()
orthen()
callback – Rory McCrossan Commented Jun 13, 2018 at 9:00 -
1
And for your next problem, don't forget to close the brackets in the selectors where you get the values (neither have a closing
]
so you'll get null for each value). – Reinstate Monica Cellio Commented Jun 13, 2018 at 9:04 - Thank you for answering. I have updated the closed bracket but unfortunately it still seems to display "email is required" no matter what(email filled in, password field blank should prompt "password is required"). – Jitrenka Commented Jun 13, 2018 at 9:52
5 Answers
Reset to default 2data
object that you are using is not defined. As suggested by 'Rory McCrossan', use the ajax in below mentioned way. For detailed info, go to this link - http://api.jquery./jquery.ajax/
Example:
$(document).ready(function() {
$('#login_button').click(function()
{
event.preventDefault();
var formData = {
'email' : $('input[email=email]').val(),
'password' : $('input[password=password]').val()
};
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : formData,
dataType : 'json',
success : function(data){
if (data.success == false)
{
if(data.errors.email)
{
//toastr.error(''+data.errors.email+'', 'Oops!');
alert('Email error');
}
else if(data.errors.password)
{
//toastr.error(''+data.errors.password+'', 'Oops!');
alert('Password Error');
}
}
else
{
//toastr.success('Works!', 'WooHoo!');
alert('Works.');
}
});
}
});
});
The response or data is available in the success callback of $.ajax
call.
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : formData,
dataType : 'json'
}).done(resp => {
// You IF statements go here.
}).fail(error => {
});
Look at the docs at: jQuery
Model ajax function looks like this
$.ajax({
url : 'wopiEditor',
data : {
data : data,
reqType : "initEditFile",
access_token : "DEADBEEFDEADBEEFDEADBEEF"
},
type : 'POST',
dataType : 'text',
success : function(response) {
},
error : function(request, textStatus, errorThrown) {
console.log(request, textStatus, errorThrown);
}
});
In your code, the data is defined anywhere. So it is giving error.
Add success handler like this...
$.ajax({
type : 'POST',
url : 'ajax/proclogin.php',
data : formData,
dataType : 'json',
success: function (response) {
console.log(response);
}
});
concerning your issue now with validation of email and password
'email' : $('input[email=email]').val(),
'password' : $('input[password=password]').val()
this -> input[email=email] is targeting an input element with an "email" attribute which isnt a valid input attribute, your looking for name/type ect.
'email' : $('input[name=email]').val(),
'password' : $('input[name=password]').val()
so you are targeting all input elements that have the name attribute of email or password. then make sure you have name=email on your input element
<input type="email" name="email">
<input type="text" name="password">
hopefully this solution helps you
本文标签: javascriptData is not definedStack Overflow
版权声明:本文标题:javascript - Data is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745468461a2659636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论