How do I get a response from the server?

  • 0
    What is my situation.
    There is a js file on the client side, from which I want to send a request to the server, where the php file should process the request and give a response.
    But no matter how hard I try, I can't send an ajax request. What am I doing wrong?
        url: 'http://localhost/server_php/index.php',
        type: "POST",
        data: { site: "test" },
        success: function (responseData){
          $LOG_G.append('<p>php: Good</p>');
        error: function(){
          $LOG_G.append('<p>php: error</p>');

    In index.php :
      header('Access-Control-Allow-Origin: *');
      header('Content-type: application/json');
      echo "I get site = ".$_POST['site'];

    In $ LOG_G it gives me - "php: Good"
    but the console gives me null.

    Why? I do not understand yet = (
    JavaScript Anna Lane, Jul 16, 2019

  • 1 Answers
  • 0
    echo json_encode (['site' = & gt; $ _POST ['site']]);

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!