error using text return type in $.post callback
-
12-06-2021 - |
Pregunta
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.
Solución
If you are echoing text/string, you can simply ignore the text
option in ajax call, try this:
$.post('ajaxupdate.php', data, handleAjaxResponse);
Otros consejos
$.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);
};
} //
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow