I have a form validation class that returns an error array.
$.ajax({
type: "POST",
url: "/oop/register.php",
data: data,
success: function(response){
alert(response);
var emptyDiv= $('<div></div>');
jQuery.each(response, function(i, val){
emptyDiv.clone().insertAfter('ul#state-list li:nth-child(' + val + ')');
});
$('#signupalert').append(emptyDiv);
$('#signupalert').fadeIn('slow');
}});
The alert shows the response is an array of errors but in the console i get the following error:
Uncaught TypeError: Cannot use 'in' operator to search for '209' in Array
(
[0] => username is required
[1] => first_name is required
[2] => last_name is required
[3] => password is required
[4] => confirm_password is required
...<omitted>...)
I would suggest to use normal for
instead of jQuery.each
maybe the returned response is not javascript array object
$.ajax({
type: "POST",
url: "/oop/register.php",
data: data,
success: function(response){
response = jQuery.parseJSON(response);
alert(response);
var emptyDiv= $('<div></div>');
for(var x = 0; x < response.length; x++) {
emptyDiv.clone().insertAfter('ul#state-list li:nth-child(' + response[x] + ')')
}
$('#signupalert').append(emptyDiv);
$('#signupalert').fadeIn('slow');
}});
Also to make sure that response[x]
is the string value which you are looking for try alert it; or you can also check the real value of response in Dev mode of your browser or if you have some tools like Fiddler use that one, just to make sure the response value is either a normal array or an array object