jQuery & JSON – Make JSON POST request using jQuery.post()

We can also make JSON POST request using the jQuery.post() function. If you want to use GET request. please refer to the previous post.
jQuery & JSON – Make JSON GET request using jQuery.getJSON()

Update @ 2011-10-13: This example does not work for Cross Domain POST request since the Current jQuery version(1.6.4) does not allow a Cross Domain POST request. For more information, you can refer to Mark Needham – jQuery: $.post, ‘jsonp’ and cross-domain requests

So if you want to make a Cross Domain GET request. Take a look on the following post.
jQuery & JSON – Make Cross Domain Request Using jQuery.getJSON() with JSONP

Here is the new server.php

  $arr1 = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
  $arr2 = array('a'=>5,'b'=>4,'c'=>3,'d'=>2,'e'=>1);
  if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    // GET request received
    if ($_GET['param1'] == 'ykyuen' && $_GET['param2'] == 'eureka') {
      print json_encode($arr1);
    } else {
      print json_encode($arr2);
  } else if($_SERVER['REQUEST_METHOD'] == 'POST') {
    // POST request received
    $arr3 = array('1st' => $_POST['param1'], '2nd' => $_POST['param2']);
    print json_encode($arr3);



    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
          param1: "ykyuen",
          param2: "eureka"
      function success(data) {
        $.each(data, function(key, val) {
          $("body").append(key + " : " + val + "<br/>");


See what you get this time.


Done =)

Reference: jQuery API – jQuery.post()

One thought on “jQuery & JSON – Make JSON POST request using jQuery.post()”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.