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 echoes the string code on jQuery success.

¿Fue útil?

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);
    };

} //

Ref: http://api.jquery.com/jQuery.trim/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top