Home > OS >  POST ajax json response to PHP
POST ajax json response to PHP

Time:10-08

I am looking for get json response from third party website. Its providing me json data when I call with ajax. Its fine. I am looking for pass same json data to my PHP file. so I can decode that json data and can store in MYSQL database. So for I am trying like this

<script>
    
    $("#diary").click(function(){        
          $.ajax({
          type:'get',
          url:'https://example.com,
          data:{
    
          },
          dataType:'json',
          success:function(result){
              console.log(result);
                $.ajax({
                   type: "POST",
                   url: "dd.php",
                   data:result,
                   dataType: "json",
                   success: function (msg) {
                       console.log(msg);
                   }
               });
    
          }
        });
    });  
    
</script>

Its working for get data from third party site but in my php page, I am not receiving proper data so json_decode function not working. May be I am not posting correct json data to PHP page. I am not able to use CURL in PHP because its giving me connection error, its possible that third party site have some security function which does not allow me to connect via CURL so ajax is the only method for get data.

My PHP file is like below

<?php
$ajax = json_decode(file_get_contents('php://input'), true);
print_r($ajax);
?>

Let me know if anyone here can help me for solve my issue. Thanks!

CodePudding user response:

You need to call JSON.stringify() on the result and pass that through data in your ajax request. You are just sending a string that happens to be JSON data. In PHP you can call json_decode() on $_POST['data'] and you should have your data.

  • Related