-2

I am trying to return a value with ajax for succes and fail but it only returns undefined. This is my script

function loginCheck(email, password, id){
    $.get("inc/ajax/login_check.php",
        {email:email, password:password},
        function(html){
            if(html == 0){
                return 0;
            }
            else{
                return 1;
            }
        }
    );
}
Sinan Samet
  • 5,034
  • 11
  • 40
  • 80
  • What returns undefined? – 0x499602D2 Mar 25 '13 at 23:04
  • @David: The OP's `loginCheck` function does (implicitly). He's using `return` within his success callback, and apparently expecting that to magically have some effect on the outer function's return value. – T.J. Crowder Mar 25 '13 at 23:04
  • I want it to return either 1 or 0. So the only way is to use $.ajax? – Sinan Samet Mar 25 '13 at 23:05
  • @SinanSamet: Your question is answered by [the answer to the question linked above](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function). – T.J. Crowder Mar 25 '13 at 23:06
  • it's an asyncronous event handler it does return 0 or 1 just not at the stack frame you expect – Rune FS Mar 25 '13 at 23:08
  • @RuneFS: No, it doesn't. – T.J. Crowder Mar 25 '13 at 23:09
  • @T.J.Crowder When were the specifications for JavaScript changed? The specs used to say that _every_ function returns a value. if there's no explicit return statement then null is returned but _every_ function returns a value. In this particular case you can't use that value but that's not what I said – Rune FS Mar 26 '13 at 07:46
  • 1
    @RuneFS: No, even the 3rd edition spec (1999) differentiated between the three possible result states (`throw`, `return`, and `normal`), see §13.2.1 in [the PDF](http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf) (warning: it's big). But it's spec-speak, really, because the *expression* calling a function always has a value (of course; expressions always have values). If the function doesn't have any `return`, the result of the expression is `undefined` (not `null`). – T.J. Crowder Mar 26 '13 at 07:52
  • 1
    @T.J.Crowder you are indeed correct if should have been undefined and not null, that does not change the point though. Every function call in JS returns a value (one of which are undefined). 13.2.1 states that if nothing is thrown and nothing is explicitly returned (result.type == return) then _return_ `undefined`. The function in question will however return either 0 or 1 and will always return one of them. There's no way to get a hold of the returned value but that does not change that it's returned – Rune FS Mar 26 '13 at 10:10
  • @RuneFS: I never said differently. In fact, I believe I said exactly that in my reply to David. I don't know what you're going off about, but I never said calling `loginCheck` did anything but result in `undefined` (I said it did), and I never said his success callback didn't return a value (I said it did, just that that has nothing to do with `loginCheck`'s return value). – T.J. Crowder Mar 26 '13 at 21:54
  • @T.J.Crowder you replied to me "no it doesn't", since I was talking about the anonymous function (function(html)...) then if you were talking about something else I can see why we do not understand each other :) Have a nice day – Rune FS Mar 27 '13 at 07:14
  • @RuneFS: I think we do understand each other. You said "it's an asyncronous event handler it does return 0 or 1 just not at the stack frame you expect" I took "it" to mean `loginCheck`, but I think now you meant the success handler. Best to be clear about these things, esp. when the OP clearly isn't. :-) – T.J. Crowder Mar 27 '13 at 07:46
  • @T.J.Crowder and OP Agreed. loginCheck returns undefined (and not the result of the anonymous event handler as it would seem OP expects) – Rune FS Mar 27 '13 at 07:57

1 Answers1

0

Please, please, please do not use GET as your method for user and passwords, security-wise it's a terrible idea.

That being said, to your question:
$.get is a shorthand for $ajax. Sometimes, it is a much better practice to declare the data's reference in your objects, that way you make sure it is being sent correctly. Try using $ajax with the full correct syntax, that goes like this:

$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});

Example:

$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).success(function( msg ) {
alert( "Data Saved: " + msg );
});

Full documentation: http://api.jquery.com/jQuery.ajax/

Ryoku
  • 734
  • 1
  • 5
  • 18
  • 4
    This is not an answer to the question. – T.J. Crowder Mar 25 '13 at 23:09
  • 2
    POST is not secure either. – bfavaretto Mar 25 '13 at 23:09
  • Then what would be secure? – Sinan Samet Mar 25 '13 at 23:11
  • 1
    Indeed it is not secure either, but sending unencrypted passwords in a GET which can be easily picked up and/or mistakenly saved by a browser is just outright careless. – Ryoku Mar 25 '13 at 23:14
  • If you want secure you could just go and encrypt all your data in-browser before sending it over https. And it would still be vulnerable. Unless you are in a vpn you can't really fight off a man in the middle attack, for example. But that doesn't mean building layers of security will not work. In fact, applying basic security to your code will stop most attacks. – Ryoku Mar 26 '13 at 15:29