I know there has been a thousand topics on this error, but none seem to match my issue. The usual answers appear to be invalid json or a problem with the datatypes in the ajax call. All json I send back is from php's json_encode.
My problem seems to be a bit weirder:
I am creating an application and this runs perfectly fine on the server at work (windows server 2008, IIS 7, PHP 5.3), and even locally on a work pc using WAMP, and on my home server which is windows 7 with IIS 7 and PHP 5.4.
However, recently when copying my application over from work to home (via Dropbox - no code changes whatsoever), suddenly every single ajax call made by jQuery is not working correctly... but it also does;
- using console.log(data) I can see that what is being returned is exactly what should be.
- but any if statement etc doesn't recognise the value
For example, my login script echos true from php (interpreted by JS as '1') if logged in, and I can see this 1 being sent back with console.log - but JS then doesn't recognise this when I use (data == 1) or data == '1', I'm baffled as to how JS can recognise data in console.log, but not in an if statement on the next line.
I get this same problem for absolutely any ajax call on any page of the application, whether JSON.parse, $.parseJSON or just standard post is being used to decipher the return value. I have also tried using dataType json for these calls with no success. When I do try using json datatype I get 'Uncaught SyntaxError: Unexpected token', but no errors at all for standard post and post backs. All calls work perfectly on the work server.
Here is the ajax success of my login call:
success: function(data) {
console.log(data);
if (data == 1) {
window.location = (window.lasturl == "" ? "index.php" : window.lasturl);
} else {
$form.after('failed to login, username and password may be incorrect - please try again');
}
Where $form is set to the login form.
console.log shows: 1 but the data == 1 if statement is not performed, and the else statement is.
It's doing my head in!
Any ideas?