I have an Ajax get request using Jquery, which on the server side has no special headers, and so just returns text/html.
$.get("index.php?action=get_current_page", function (response) {
when I then do an alert:
alert(response);
the value shows: inbox
But why then does this on the very next line evaluates to false:
if (response == "inbox") {
Any ideas? Do you need more info? Thanks.
The whole snippet is:
$.get("index.php?action=get_current_page", function (response) {
alert(response);
if (response == "inbox") {
alert("hello");
loadInbox();
}
And on the server side I am simpyl echoing a session variable:
echo $_SESSION['current_page'];
It most likely contains spaces or other invisible chars. If don't have control of server side output, then you can use JavaScript to replace invisible characters. Otherwise, if you do have control of server side output. You can do this on the server side.
If you have server-side access to the PHP script, then trim()
will do the job of replacing white space from the beginning and end of the string.
$response = trim($response);
echo $response;
If you only have access to the client side then use JavaScript. More specifically, String.prototype.replace()
will get the job done by replacing every occurrence of characters that are not a-z
or A-Z
response = response.replace(/[^a-zA-Z]/g,'');