-2

PHP script:-

<?php
header('Access-Control-Allow-Origin: *');
echo "Done";
?>

AJAX script:-

$(document).ready(function(){
$('#submit').click(function(){
    $.ajax({
        type : "POST",
        url : "http://127.0.0.1/ionic/retri.php",
        success : function(data){
            alert(data);
            $('#card').text(data);
        }
    })
 });
});

HTML:-

 <button id="submit" class="button button-block button-positive">
     Submit
    </button>
    <div class="card">
      <div id="card" class="item item-text-wrap">

      </div>
    </div>

I have just started learning AJAX and wrote this script just to echo simple text and update the html div but I am not getting any output.

2 Answers2

0

These steps will pinpoint almost all basic AJAX related problems:

  1. If you have a console tool, like firebug for Firefox, you should check if the request is made. Pressing F12 (on windows) often brings them up. Learn how this console works and you'll save yourself huge amounts of time trying to figure out what's going wrong.

  2. If the request is made, you should check if the file exists. Consoles will show what kind of headers you receive (200,301 etc). 404 means you linked incorrectly, sometimes the request will be colored red instead of black as an extra hint.

  3. If the request is also correct, look for the response tab. Here you'll see what PHP has output. If it's blanco, it might be a white screen of dead, try to remove all PHP and only let your done be there as plain text. If it is outputting, check if it matches your expectation of the result. In case you request it as type JSON, check if it actually is JSON.


In your case, I think you get white screen of death. Also, the request header is not needed at all. Use PHP like it's not an AJAX request (unless you want to change specific actions based on wether or not you're using AJAX or not, but this is not the case in your code).

Martijn
  • 15,791
  • 4
  • 36
  • 68
0

Try changing your PHP Script....

    <?php
        //THIS SHOULD BE ENOUGH FOR YOUR PHP SCRIPT TO RETURN A RESPONSE
        $response   = "<strong>This is a response from the Server</strong>";
        die($response); // ECHO BACK THE CONTENTS ON THE $response VARIABLE

    ?>
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • 1
    Wow, `die()`? Please don't do that. A simple `echo` will be better. Never `die()` without good reason. – Martijn May 04 '16 at 18:53
  • Have you tried `return "Done";`? – P. Gearman May 04 '16 at 18:55
  • Will fail as well, you´re not in a function, so you return a value to nothing. Just use plain echo. – Martijn May 04 '16 at 18:58
  • 3
    @KshitijPandey you accepted an answer that wasn't even based on what you just said here in comments [*"Finally getting the output. restarted xampp and got the output"*](http://stackoverflow.com/questions/37035556/my-php-script-is-not-returning-data-to-ajax-script#comment61621937_37035556) why is that? You're sending out the wrong message here. – Funk Forty Niner May 04 '16 at 18:58
  • I got the output on using die() So I marked it as right answer – Kshitij Pandey May 04 '16 at 19:00
  • DON'T USE `DIE()`. THIS DOESN'T FIX THE PROBLEM, IT HIDES IT. `Die()` is a function you use when you are absolutaly sure what you're doing. – Martijn May 04 '16 at 19:00
  • Use `echo $response; exit;` – RiggsFolly May 04 '16 at 19:00
  • OH MY GOD. Don;t use exit as well. `Just. Use. Echo.` There are too many bad practices here X_X – Martijn May 04 '16 at 19:01
  • @RiggsFolly even just `exit($response);` to cut it shorter as a one-liner. or `echo $response = "..."; exit;` – Funk Forty Niner May 04 '16 at 19:02
  • @Martijn Why is it wrong to use `die()` if there is nothing to be executed afterwards? – Mikey May 04 '16 at 19:02
  • @RiggsFolly `exit()` and `die()` are aliases. – Mikey May 04 '16 at 19:04
  • 1
    Because there is nothing afterwards *now*. Code might change, other people might add stuff. `die()` is a last resort kind of function. Say you improve this particular file and add a few functions in it. Code should not exit halfway through a page. Problems should be caught and handled properly by error handling fucntions – Martijn May 04 '16 at 19:04
  • 1
    For those wondering about the differences between die and exit http://stackoverflow.com/a/27851270/ and I quote: *"die() and exit() are different in other languages but in PHP they are identical."* – Funk Forty Niner May 04 '16 at 19:06
  • @Martijn *"Problems should be caught and handled properly by error handling fucntions"* - True, and as [I stated to the OP earlier...](http://stackoverflow.com/questions/37035556/my-php-script-is-not-returning-data-to-ajax-script#comment61621894_37035556) which probably caught something but failed to say so. – Funk Forty Niner May 04 '16 at 19:08