user3581348
user3581348

Reputation:

no response ajax request

I am trying to make a simple login with ajax. The problem i have is that i do not get any response from my request. When I try to use

myurl.com/login.php is_ajax=1&username=test&password=test

I get a succes message back.

 $(document).ready(function(){

    $("#submit").click(function(){   
var form_data = {
       usernm: $("#username").val(),
       passwd: $("#password").val(),
      is_ajax: 1
    };

      $.ajax({
      type:"POST",
      url: "myurl.php" , 
      data: form_data,
      succes: function (response)
      {
        if (response == 'succes'){
        window.location="myurl.html";
      }
        else{
        alert("wrong username password combination")
       }
        }

      });
return false;

  });    
 });

my php:

$is_ajax = $_REQUEST['is_ajax'];
if(isset($is_ajax) && $is_ajax)
 {
$uName = $_REQUEST ['username'];
$pWord = $_REQUEST ['password'];

if($uName == 'test' && $pWord == 'test'){
echo 'succes';

}
else{
echo 'false';
}
}   

Upvotes: 0

Views: 101

Answers (3)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

Replace succes with success

$.ajax({
  type:"POST",
  url: "myurl.php" , 
  data: form_data,
  success: function (response){
    if (response == 'success'){
      window.location="myurl.html";
    }else{
      alert("wrong username password combination");
    }
  }
});

also change the data attribute names

var form_data = {
  username: $("#username").val(),
  password: $("#password").val(),
  is_ajax: 1
};

Upvotes: 0

alpham8
alpham8

Reputation: 1362

If you are doing it with $.ajax, then your datatype is wrong. It must be application/x-www-form-urlencoded.

So please change your JS part to this:

 $(document).ready(function(){

$("#submit").click(function(){   
var form_data = {
   usernm: $("#username").val(),
   passwd: $("#password").val(),
  is_ajax: 1
};

      $.ajax({
      type:"application/x-www-form-urlencoded",
      url: "myurl.php" , 
      data: form_data,
      success: function (response)
      {
        if (response == 'success'){
        window.location="myurl.html";
      }
        else{
        alert("wrong username password combination")
       }
        }

      });
return false;

  });    
 });

Or, if you are not sure about the correct MIME type, then just use this jQuery command:

$(document).ready(function(){

$("#submit").click(function(){   
var form_data = {
   usernm: $("#username").val(),
   passwd: $("#password").val(),
  is_ajax: 1
};

      $.post("myurl.php",
             form_data,
             function (response)
             {
                 if (response == 'success'){
                     window.location="myurl.html";
                 }
                 else{
                     alert("wrong username password combination")
                 }
             });
return false;

  });    
 });

And please change also the following lines of your PHP code

$uName = $_REQUEST ['username'];
$pWord = $_REQUEST ['password'];

To this:

$uName = $_REQUEST ['usernm'];
$pWord = $_REQUEST ['passwd'];

Because these are the correct keys, wich you send to your php server file. Also, there´s no need for defining an "is_ajax" key, because every XMLHttpRequest has set the "X_REQUESTED_WITH" header to "XMLHttpRequest". So, you can request it in your php server part e. g. with:

if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest')
{
    // it was an ajax XHR request!
}

Upvotes: 0

Think Different
Think Different

Reputation: 2815

you should use

$uName = $_REQUEST ['usernm'];
$pWord = $_REQUEST ['passwd'];

INSTEAD of

$uName = $_REQUEST ['username'];
$pWord = $_REQUEST ['password'];

in your php file and change succes to success

Upvotes: 1

Related Questions