user3659748
user3659748

Reputation: 9

send a text with ajax and receive a response

Hmm I have a little problem with that :

  $.ajax({
      type: 'POST',
      data: {
          name : 'aatrox'
      },
      dataType: "json",
      async:false,
      url: 'appliserv/testsendajax.php',
      success: function(data){
          console.log(data);
          alert('good');
      },
      error: function(data){
          console.log(data);
          alert(fail);
      }
  });

the text in the alert is always fail .... in my serv :

$text = $_POST['name'];

echo $text;

and I don't understand that. Thanks (sry if my english isn't good)

Upvotes: 0

Views: 62

Answers (3)

user1423454
user1423454

Reputation: 41

You already got your answer, I just want to add that using

async: false

is a bad practice when dealing with ajax calls, it freezes your browser while waiting for the response. If it's possible you should try to avoid this behavior and use instead the ajax callbacks properly; however if you really want to block the UI you can try to use something like http://malsup.com/jquery/block/

Upvotes: 0

hindmost
hindmost

Reputation: 7195

Your server-side script output plain text. So you have to change value of dataType option.

Change this:

dataType: "json"

to this:

dataType: "text"

Upvotes: 2

PeterKA
PeterKA

Reputation: 24638

Quite likely you're getting a parse error. This is because your ajax request is expecting JSON but you're are returning a plain string. Either return JSON from your PHP script or change the dataType to text:

dataType: "text",

Also be sure to change alert(fail); to:

alert('fail');

String literals must be delimited or variables have to be initialized before they are used.

Upvotes: 0

Related Questions