简体   繁体   中英

How to properly retrieve a JSON string with PHP?

I am trying to send a JSON to my PHP script and for some reason is returning null in the alert message.

I've searched throughout Stack Overflow and other forums and used other people's examples but somehow I keep getting it wrong. I checked in Firebug and the request is being sent. I am very new to handling JSON queries.

Could anyone point at what I am doing wrong so that I can learn for the next time?

My JQuery Code:

  var name = $('#formName').val();
  var regno = $('#formRegNo').val();
  var charityinfo = $('#formCharityInfo').val();
  var searchimprove = $('#formSearchImprove').val();
  var finantialaid = $('#formFinantialAid').val();
  var contactname = $('#formContactName').val();
  var contactphonenumber = $('#formContactPhoneNumber').val();
  var contactfaxnumber = $('#formContactFaxNumber').val();
  var contactemail = $('#formContactEmail').val();
  var website = $('#formWebsite').val();
  var address = $('#formAddress').val();
  var postcode = $('#postCode').val();

    var arrayData = {"name":name,"regno":regno,"charityinfo":charityinfo,"searchimprove":searchimprove,"finantialaid":finantialaid,"contactname":contactname,"contactphonenumber":contactphonenumber,"contactfaxnumber":contactfaxnumber,"contactemail":contactemail,"website":website,"address":address,"postcode":postcode};
    console.log(name);

    $.ajax({  
  url: "test.php",  
  type: "POST",  
  dataType: "json",  
  contentType: "json",  
  async: false,
  data: arrayData,
 success : function(response) {                            
                    alert(response);            
              },
              error: function(){
                    alert("error");
              }
  }); //End AJAX

My PHP Code:

<?php
$return = $_POST;
echo json_decode($return);
?>

first, collect your form data in 1 step, serialize it as json:

var arrayData = $('#my_form_id').serialize();

If you post your data, you should post the appropriate post variable name with it. Add some more decriptive error handling:

$.ajax({  
      url: "/test.php",  
      type: "POST",  
      data: 'mydata = ' + arrayData,
      success : function(response) {                            
        alert(response);            
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(thrownError);
      }
    });

And then on the php side pick it up:

$mydata = $_POST['mydata'];
$arrayData = json_decode($mydata);
var_dump($arrayData);
  1. You haven't encoded the data you are sending as JSON. You are passing an object to jQuery and allowing it to encode it (as application/x-www-form-urlencoded ). You need to encode your data as JSON and pass a string to data: .
  2. json is not a content-type, application/json is.
  3. $_POST is populated from application/x-www-form-urlencoded or multipart/form-data , it doesn't contain the raw post request. You need to use php://input to get that.

First of all you don't need to create json yourself

use JSON.stringify() function function to create JSON

or you can also use the jquery serialize() function

var arrayData=$("FORMID").serialize();
$.ajax({  
  url: "test.php",  
  type: "POST",  
  dataType: "json",  
  contentType: "json",  
  async: false,
  data: arrayData,
  .....

and decode this json serverside using json_decode() function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM