error using text return type in $.post callback
-
12-06-2021 - |
Question
My callback function returns data as text, but my callback function is not recognizing it and executes always the else condition.
Here are my 'calling' parameters
$.post('ajaxupdate.php', data, handleAjaxResponse, 'text');
and my callback function is:
function handleAjaxResponse(response) {
if (response == 'worked') {
alert("hi");
}else{
console.log(response);
};
} // End of handleAjaxResponse() function.
and my php echo
es the string code on jQuery success.
La solution
If you are echoing text/string, you can simply ignore the text
option in ajax call, try this:
$.post('ajaxupdate.php', data, handleAjaxResponse);
Autres conseils
$.post('ajaxupdate.php', function(data) {
alert(data)
});
Try this cde it may help you
You may have extra white space or line break being sent
Try:
function handleAjaxResponse(response) {
if ($.trim(response) == 'worked') {
alert("hi");
}else{
console.log(response);
};
} //
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow