How to get ajax response in a variable or alert

1

Good afternoon, I'm trying to get the answer from my ajax request, but I have not been able to do so. I have this code that works perfectly to send but I do not receive the answer and I would like to have that answer in an alert.

Here the ajax petition

   var url = "enviar.php";
         $.ajax({
            type: "POST",
            url: url,
            data:{mesa:mesa,item:item,precio:precio},
            success: function(data)

            {
          $("#text").html(data);

            }
          });

            return false;
       });
     });
     </script>

Here is a fragment of my php

echo 'Added';

    
asked by juan perez 17.12.2017 в 21:57
source

1 answer

0

Friend your ajax is perfect the only thing I think is that the php is not returned information for it you have to return with echo I hope you help greetings. p>

PHP

Try this simple php and it should give you as an alert "Hola mundo"

<?php


 echo "Hola mundo"

?>

Functional example

  

NOTE It is a request to a test API.

var root = 'https://jsonplaceholder.typicode.com';

$.ajax({
  url: root + '/posts/1',
  method: 'GET'
}).then(function(data) {
  alert(JSON.stringify(data));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

In the previous example we use an ajax that returns us a promise in few words the method .then would be the same as the success of a ajax conventional.

  

NOTE: For the example we use the function JSON.stringify that transforms a JSON to string to be able to visualize it in alert .

    
answered by 17.12.2017 в 22:00