kamil
kamil

Reputation: 579

Ajax getting response undefined and Uncaught ReferenceError

Code:

query = "http://localhost:8080/working_login.php?name=" + name + "&password=" + pass;
console.log(query);

   $.ajax({
    type: "GET",
    url: query,
 dataType: "script",
 success: function(data){
            console.log(data);
            console.log("success");
        },
error:function (msg) {
            console.log("error on page");
        }           
    });

I am getting undefined in data and as I am returning 'valid_user' from the server side made in php, getting error.

Uncaught ReferenceError: valid_user is not defined
 (anonymous function)

Php server side code:

$sql = "SELECT id FROM users WHERE name ='$name' AND password = '$password';"; 
$result = mysql_query($sql) or die('Can not Register user' . $sql );
$rows = mysql_num_rows($result);
if($rows > 0)
{ 
echo("valid_user");
exit();
}
else
{exit("not valid user");} 

Upvotes: 1

Views: 5353

Answers (2)

Ryan
Ryan

Reputation: 144

You probably didn't mean to tell jQuery that you were expecting a script in return. What's being returned isn't a properly formatted script, so it's giving you an error when the browser parses as a script.

You could just do the following:

$.get("http://localhost:8080/working_login.php?name=" + name + "&password=" + pass, function(data) {
    if(data == 'success')
    {
        alert("Success");
    }
    else
    {
        alert("Failed");
    }
});

Upvotes: 0

Andreas Linden
Andreas Linden

Reputation: 12721

when using the script dataType in the jquery ajax reqeust, the returned result is interpreted as javascript code, but you return plain text

simply change dataType: "script" to dataType: "text".

Upvotes: 1

Related Questions