jQuery Ajax PHP POST method with response array[] instad sent data. How to get send values in response?












1















I am creating simple application which will insert log data into database table.



Going with jQuery Ajax POST method and JSON format for data.



My index.php has HTML and this code:



<script type="text/javascript">
jQuery(document).ready(function() {
jQuery.ajax({
type: "POST",
url: "http://www.example.com/ajax_model.php",
data: {
"log_session": "27738d7552b75ae395ae1138adf4fa60",
"log_ip": "1.2.3.4",
"log_vrijeme": "2018-11-23 01:22:47",
"log_model": "12345"
},
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(response) {
console.log(response);
alert(response);
},
error: function(error) {
console.log(error);
}
});
});
</script>


My ajax_model.php - which gives me response Array instad the data which is sent:



<?php
if(isset($_POST)){
header('Content-Type: application/json');
echo json_encode($_POST);
exit;
}
?>


I am getting result in my console.log() and alert() - Array.
How can I output my sent response to check if the data was correctly sent over Ajax?



Am I missing some brackets like or {}?
Or do I need to add something to my ajax_model.php file?



Thank you for suggestion and provided information.










share|improve this question





























    1















    I am creating simple application which will insert log data into database table.



    Going with jQuery Ajax POST method and JSON format for data.



    My index.php has HTML and this code:



    <script type="text/javascript">
    jQuery(document).ready(function() {
    jQuery.ajax({
    type: "POST",
    url: "http://www.example.com/ajax_model.php",
    data: {
    "log_session": "27738d7552b75ae395ae1138adf4fa60",
    "log_ip": "1.2.3.4",
    "log_vrijeme": "2018-11-23 01:22:47",
    "log_model": "12345"
    },
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    success: function(response) {
    console.log(response);
    alert(response);
    },
    error: function(error) {
    console.log(error);
    }
    });
    });
    </script>


    My ajax_model.php - which gives me response Array instad the data which is sent:



    <?php
    if(isset($_POST)){
    header('Content-Type: application/json');
    echo json_encode($_POST);
    exit;
    }
    ?>


    I am getting result in my console.log() and alert() - Array.
    How can I output my sent response to check if the data was correctly sent over Ajax?



    Am I missing some brackets like or {}?
    Or do I need to add something to my ajax_model.php file?



    Thank you for suggestion and provided information.










    share|improve this question



























      1












      1








      1








      I am creating simple application which will insert log data into database table.



      Going with jQuery Ajax POST method and JSON format for data.



      My index.php has HTML and this code:



      <script type="text/javascript">
      jQuery(document).ready(function() {
      jQuery.ajax({
      type: "POST",
      url: "http://www.example.com/ajax_model.php",
      data: {
      "log_session": "27738d7552b75ae395ae1138adf4fa60",
      "log_ip": "1.2.3.4",
      "log_vrijeme": "2018-11-23 01:22:47",
      "log_model": "12345"
      },
      dataType: "json",
      contentType: 'application/json; charset=utf-8',
      success: function(response) {
      console.log(response);
      alert(response);
      },
      error: function(error) {
      console.log(error);
      }
      });
      });
      </script>


      My ajax_model.php - which gives me response Array instad the data which is sent:



      <?php
      if(isset($_POST)){
      header('Content-Type: application/json');
      echo json_encode($_POST);
      exit;
      }
      ?>


      I am getting result in my console.log() and alert() - Array.
      How can I output my sent response to check if the data was correctly sent over Ajax?



      Am I missing some brackets like or {}?
      Or do I need to add something to my ajax_model.php file?



      Thank you for suggestion and provided information.










      share|improve this question
















      I am creating simple application which will insert log data into database table.



      Going with jQuery Ajax POST method and JSON format for data.



      My index.php has HTML and this code:



      <script type="text/javascript">
      jQuery(document).ready(function() {
      jQuery.ajax({
      type: "POST",
      url: "http://www.example.com/ajax_model.php",
      data: {
      "log_session": "27738d7552b75ae395ae1138adf4fa60",
      "log_ip": "1.2.3.4",
      "log_vrijeme": "2018-11-23 01:22:47",
      "log_model": "12345"
      },
      dataType: "json",
      contentType: 'application/json; charset=utf-8',
      success: function(response) {
      console.log(response);
      alert(response);
      },
      error: function(error) {
      console.log(error);
      }
      });
      });
      </script>


      My ajax_model.php - which gives me response Array instad the data which is sent:



      <?php
      if(isset($_POST)){
      header('Content-Type: application/json');
      echo json_encode($_POST);
      exit;
      }
      ?>


      I am getting result in my console.log() and alert() - Array.
      How can I output my sent response to check if the data was correctly sent over Ajax?



      Am I missing some brackets like or {}?
      Or do I need to add something to my ajax_model.php file?



      Thank you for suggestion and provided information.







      javascript php jquery ajax






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 0:45









      Andrey Lutskevich

      986818




      986818










      asked Nov 23 '18 at 0:32









      Kiki FIstrek NoviKiki FIstrek Novi

      287




      287
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You aren't sending JSON so get rid of:



           contentType: 'application/json; charset=utf-8',


          That will cause $_POST to be empty as the default contentType is:



          application/x-www-form-urlencoded; charset=UTF-8





          share|improve this answer


























          • Woow! It works after I removed the suggested "contentType". I spend two hours figuring out what could I got wrong. Thank you very much

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:36











          • @KikiFIstrekNovi - You should mark the answer as accepted. It makes it easier for people to find the right answer later and the contributors appreciate it.

            – Difster
            Nov 23 '18 at 0:38











          • Moreover, I would like to know why did I get the null body in some responses while trying to figure it out? Like, HTTP 200 and status Ok, but response was empty?

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:58











          • Not really sure to be honest and am also guessing you tried lots of things in the php at same time

            – charlietfl
            Nov 23 '18 at 1:02













          • Yes, exactly I did. Maybe messed up something among with it.

            – Kiki FIstrek Novi
            Nov 23 '18 at 1:05













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439409%2fjquery-ajax-php-post-method-with-response-array-instad-sent-data-how-to-get-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You aren't sending JSON so get rid of:



           contentType: 'application/json; charset=utf-8',


          That will cause $_POST to be empty as the default contentType is:



          application/x-www-form-urlencoded; charset=UTF-8





          share|improve this answer


























          • Woow! It works after I removed the suggested "contentType". I spend two hours figuring out what could I got wrong. Thank you very much

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:36











          • @KikiFIstrekNovi - You should mark the answer as accepted. It makes it easier for people to find the right answer later and the contributors appreciate it.

            – Difster
            Nov 23 '18 at 0:38











          • Moreover, I would like to know why did I get the null body in some responses while trying to figure it out? Like, HTTP 200 and status Ok, but response was empty?

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:58











          • Not really sure to be honest and am also guessing you tried lots of things in the php at same time

            – charlietfl
            Nov 23 '18 at 1:02













          • Yes, exactly I did. Maybe messed up something among with it.

            – Kiki FIstrek Novi
            Nov 23 '18 at 1:05


















          2














          You aren't sending JSON so get rid of:



           contentType: 'application/json; charset=utf-8',


          That will cause $_POST to be empty as the default contentType is:



          application/x-www-form-urlencoded; charset=UTF-8





          share|improve this answer


























          • Woow! It works after I removed the suggested "contentType". I spend two hours figuring out what could I got wrong. Thank you very much

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:36











          • @KikiFIstrekNovi - You should mark the answer as accepted. It makes it easier for people to find the right answer later and the contributors appreciate it.

            – Difster
            Nov 23 '18 at 0:38











          • Moreover, I would like to know why did I get the null body in some responses while trying to figure it out? Like, HTTP 200 and status Ok, but response was empty?

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:58











          • Not really sure to be honest and am also guessing you tried lots of things in the php at same time

            – charlietfl
            Nov 23 '18 at 1:02













          • Yes, exactly I did. Maybe messed up something among with it.

            – Kiki FIstrek Novi
            Nov 23 '18 at 1:05
















          2












          2








          2







          You aren't sending JSON so get rid of:



           contentType: 'application/json; charset=utf-8',


          That will cause $_POST to be empty as the default contentType is:



          application/x-www-form-urlencoded; charset=UTF-8





          share|improve this answer















          You aren't sending JSON so get rid of:



           contentType: 'application/json; charset=utf-8',


          That will cause $_POST to be empty as the default contentType is:



          application/x-www-form-urlencoded; charset=UTF-8






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 0:36

























          answered Nov 23 '18 at 0:34









          charlietflcharlietfl

          139k1387120




          139k1387120













          • Woow! It works after I removed the suggested "contentType". I spend two hours figuring out what could I got wrong. Thank you very much

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:36











          • @KikiFIstrekNovi - You should mark the answer as accepted. It makes it easier for people to find the right answer later and the contributors appreciate it.

            – Difster
            Nov 23 '18 at 0:38











          • Moreover, I would like to know why did I get the null body in some responses while trying to figure it out? Like, HTTP 200 and status Ok, but response was empty?

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:58











          • Not really sure to be honest and am also guessing you tried lots of things in the php at same time

            – charlietfl
            Nov 23 '18 at 1:02













          • Yes, exactly I did. Maybe messed up something among with it.

            – Kiki FIstrek Novi
            Nov 23 '18 at 1:05





















          • Woow! It works after I removed the suggested "contentType". I spend two hours figuring out what could I got wrong. Thank you very much

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:36











          • @KikiFIstrekNovi - You should mark the answer as accepted. It makes it easier for people to find the right answer later and the contributors appreciate it.

            – Difster
            Nov 23 '18 at 0:38











          • Moreover, I would like to know why did I get the null body in some responses while trying to figure it out? Like, HTTP 200 and status Ok, but response was empty?

            – Kiki FIstrek Novi
            Nov 23 '18 at 0:58











          • Not really sure to be honest and am also guessing you tried lots of things in the php at same time

            – charlietfl
            Nov 23 '18 at 1:02













          • Yes, exactly I did. Maybe messed up something among with it.

            – Kiki FIstrek Novi
            Nov 23 '18 at 1:05



















          Woow! It works after I removed the suggested "contentType". I spend two hours figuring out what could I got wrong. Thank you very much

          – Kiki FIstrek Novi
          Nov 23 '18 at 0:36





          Woow! It works after I removed the suggested "contentType". I spend two hours figuring out what could I got wrong. Thank you very much

          – Kiki FIstrek Novi
          Nov 23 '18 at 0:36













          @KikiFIstrekNovi - You should mark the answer as accepted. It makes it easier for people to find the right answer later and the contributors appreciate it.

          – Difster
          Nov 23 '18 at 0:38





          @KikiFIstrekNovi - You should mark the answer as accepted. It makes it easier for people to find the right answer later and the contributors appreciate it.

          – Difster
          Nov 23 '18 at 0:38













          Moreover, I would like to know why did I get the null body in some responses while trying to figure it out? Like, HTTP 200 and status Ok, but response was empty?

          – Kiki FIstrek Novi
          Nov 23 '18 at 0:58





          Moreover, I would like to know why did I get the null body in some responses while trying to figure it out? Like, HTTP 200 and status Ok, but response was empty?

          – Kiki FIstrek Novi
          Nov 23 '18 at 0:58













          Not really sure to be honest and am also guessing you tried lots of things in the php at same time

          – charlietfl
          Nov 23 '18 at 1:02







          Not really sure to be honest and am also guessing you tried lots of things in the php at same time

          – charlietfl
          Nov 23 '18 at 1:02















          Yes, exactly I did. Maybe messed up something among with it.

          – Kiki FIstrek Novi
          Nov 23 '18 at 1:05







          Yes, exactly I did. Maybe messed up something among with it.

          – Kiki FIstrek Novi
          Nov 23 '18 at 1:05




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439409%2fjquery-ajax-php-post-method-with-response-array-instad-sent-data-how-to-get-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python