2
The code I made was working normally but suddenly it started to behave differently. I’ve done all the debug possible, take a look
$( function(){
// When submit it
$('.frms').not('#form-newsletter').on('submit', function(){
e.preventDefault();
});
$('.submit').on('click', function(e){
var id_form = $(this).parents('.frms').attr('id'),
data = $('#' + id_form).serialize();
$.ajax({
url: './ajax/submit.php',
data: data,
type: 'post',
dataType: 'json',
beforeSend: function(){
$('#' + id_form).find('.loading').fadeIn();
$('#' + id_form + ' .submit').attr("disabled", 'disabled');
},
success: function(callback){
if(callback.error){
$('.success').remove();
$('.error').remove();
var split = callback.error.split('#');
if(split[1] == 'show_message'){
$('body').append('<div class="error">' + split[0] + '</div>');
}else{
$('#' + id_form + ' #' + split[1]).focus();
$('#' + id_form + ' #' + split[1]).on('blur', function(){
if($(this) != ''){
return true;
}
});
}
$('.error').on('click',function(){
$(this).fadeOut('slow');
});
}else{
$('.success').remove();
$('.error').remove();
split = callback.success.split('#');
if(split[1] == 'success'){
$('#' + id_form).find('input[class!="noclear"]').val('');
$('#' + id_form).find('textarea').val('');
$('#' + id_form).find('select').val('');
$('body').append('<div class="success">' + split[0] + '</div>');
}else if(split[1] == 'redirecty'){
$('body').append('<div class="success">' + split[0] + '</div>');
setTimeout(function(){
window.location.href = 'my-account';
},2000);
}
$('.success').on('click',function(){
$(this).fadeOut('slow');
});
}
},
complete: function(){
$('#' + id_form).find('.loading').fadeOut();
$('#' + id_form + ' .submit').removeAttr("disabled");
}
});
return false;
});
});
And on the page submit.php
which receives the data it returns the output:
if(!empty($message)):
echo json_encode($message);
endif;
This was OK until the script started sending together with Json the full html page.
Any suggestions of what might be causing this problem?
The problem is in javascript or php?
– rray
Isso era ok até
.. What do you mean by that? Does it mean that up to a certain point it worked and after a given moment the problem began? Simply go back to the point where it worked and consider which later changes may be the cause of the problem.– Daniel Omine
Where is the HTML?...
– Leonardo
People, HTML is not important because it is dynamic. The problem is either in js or in the server response. Can you identify something wrong? I’ve debugged with all the options I have, I’ve isolated the part of the page script you’re asking to see if this was the problem, but nothing. The page Submit.php, which is the one that receives the data, after all the data processing it responds with $message = ['error' => 'message_callback. #show_message']; if(!Empty($message)): echo json_encode($message); endif;
– Samuel Dos Santos
I can’t identify where this HTML is coming from. The message is being sent correctly, but above comes the full HTML of the page that should not come.
– Samuel Dos Santos
@Samueldossantos Do a very minimal test to isolate the problem. Your code is full of unnecessary things.
– Pablo Almeida
Barter
json
forjsonp
indataType
.– Diego Souza
I will conduct new tests based on the suggestion and put the result.
– Samuel Dos Santos
Put a
console.log(callback)
right after the success function and open the browser console to check how PHP is returning the response. It must be pure JSON, can not have any type of HTML or text outside the JSON structure, otherwise jQuery will not recognize and error will happen.– Dian Carlos