Jump to content

Recommended Posts

Hi there

 

Say I request some data from a file called server.php like so:

 

var url = "server.php";
request.open("POST", url, true);
request.onreadystatechange = updatePage;
request.send(JSONencodedString);

 

How do I grab that JSON data in server.php?

(I know I could do it easily using a GET-method but I specifically want to use POST)

 

Many thanks!

put this in server.php to see what's coming through.

echo "", print_r($_POST), "";

 

Note:.. you must use your AJAX callback function updatePage to view this data.  Simply going to server.php will do nothing.

Hey, using your code outputs:

 

Array ( )

1

 

My index.php looks like this:

 

<script language="javascript" type="text/javascript">

//Setup xmlHTTP object
var xmlHTTP = new XMLHttpRequest();

var test = { "test":"test2" } 

function callServer() {
  var url = "ajax/server.php";
  xmlHTTP.open("POST", url, true);
  xmlHTTP.onreadystatechange = updatePage;
  xmlHTTP.send(test);
}

function updatePage() {
  //Check for a valid readyState
  if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {
    var response = xmlHTTP.responseText;
    document.getElementById("testDiv").innerHTML = response;
  }
}

</script>

<div id="testDiv">Something here</div>

<a href="javascript:callServer();">Call server</a>

 

And my server.php looks like this:

 

<?php

echo "<pre>".print_r($_POST)."</pre>";

?>

 

Okay I'm reopening this one becuase now I've run into a problem I really can't figure out.

 

My index.php looks like this

 

<script language="javascript" type="text/javascript">

$(document).ready(function(){

  $(".ajaxlink").click(function() {
    callServer();
    return false; //Stop link from redirecting
  });

});

var test = { "testName": "testValue" }
var testJSON = JSON.stringify(test);

function updatePage(data) {
  document.getElementById("testDiv").innerHTML = data;
}

function callServer() {
$.ajax({
   type: "POST",
   url: "ajax/server.php",
   data: testJSON,
   success: function(data) {
     updatePage(data);
   },
   //Upon error, output message containing a little info on what went wrong
   error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert('An Ajax error occured\ntextStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown + '\nstatus = ' + XMLHttpRequest.status);
   }
});
}

</script>

<div id="testDiv">Something here</div>

<a href="test1.htm" class="ajaxlink">Link!</a> <br>

 

This basically runs the callServer() function when you click the "Link!". It then sends the test json data, that is { "testName": "testValue" } to server.php. Firebug reports that the json-data is indeed sent to the server.php.

 

My server.php looks like this:

 

<?php

print_r($_POST);

?>

 

This returns:

 

Array
(
)

 

Which should be a simply name/value pair array with the JSON data instead. What am I doing wrong here?

I'm using Apache 2.2 and PHP 5.3.1

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.