sorenchr Posted February 24, 2010 Share Posted February 24, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/193273-grabbing-data-on-server-from-a-post-json-request/ Share on other sites More sharing options...
Zane Posted February 24, 2010 Share Posted February 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193273-grabbing-data-on-server-from-a-post-json-request/#findComment-1017705 Share on other sites More sharing options...
sorenchr Posted February 24, 2010 Author Share Posted February 24, 2010 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/193273-grabbing-data-on-server-from-a-post-json-request/#findComment-1017709 Share on other sites More sharing options...
sorenchr Posted February 25, 2010 Author Share Posted February 25, 2010 Shameless bump Quote Link to comment https://forums.phpfreaks.com/topic/193273-grabbing-data-on-server-from-a-post-json-request/#findComment-1017956 Share on other sites More sharing options...
sorenchr Posted February 25, 2010 Author Share Posted February 25, 2010 Nevermind, I found out how to handle this with jQuery. Marking it solved. Quote Link to comment https://forums.phpfreaks.com/topic/193273-grabbing-data-on-server-from-a-post-json-request/#findComment-1017990 Share on other sites More sharing options...
sorenchr Posted February 25, 2010 Author Share Posted February 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193273-grabbing-data-on-server-from-a-post-json-request/#findComment-1018065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.