-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.

9
  • did you see if your PHP code is getting the request?, this code should basically work, it is probably config problems, how do you run your php? in what platform? Commented May 4, 2016 at 18:43
  • I am using XAMPP to run PHP Commented May 4, 2016 at 18:45
  • What does your <form> look like? Commented May 4, 2016 at 18:46
  • I am not using the form tag Commented May 4, 2016 at 18:46
  • so why don't you base yourself on the model answer you accepted for your other question which looks to be similar stackoverflow.com/q/36989766 Commented May 4, 2016 at 18:49

2 Answers 2

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

    ?>
Sign up to request clarification or add additional context in comments.

14 Comments

Wow, die()? Please don't do that. A simple echo will be better. Never die() without good reason.
Have you tried return "Done";?
@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" why is that? You're sending out the wrong message here.
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
For those wondering about the differences between die and exit stackoverflow.com/a/27851270 and I quote: "die() and exit() are different in other languages but in PHP they are identical."
|
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).

2 Comments

I checked in the console. There was no error. The status was 200
In my edit Ive added the white screen of death part.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.