Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Tuesday, May 23, 2017

jQuery AJAX call with JSON

Make AJAX call with jQuery and then receive and parse JSON string.


<html>
<head>
    <title>Making AJAX call with jQuery 3+</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        makeAJAX();

        function makeAJAX() {
            var request = $.ajax({
                url: '/ajax.php',
                method: "GET",
                data: {
                    id: 21,
                    minprice: 10,
                    maxprice: 1000
                },
                beforeSend: function(xhr) {
                    // set security token in AJAX request header
                    // xhr.setRequestHeader('X-Sec-Header','<?php echo $_SESSION["sectok"]; ?>');

                    // show loader
                    //$("#imgload").show();
                }
            });

            request.done(function(msg) {
                console.log(msg.films[0]);
                alert("name " + msg.films[0].name);
            });

            request.fail(function(jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });

            request.always(function() {
                // hide loader
                //$("#imgload").hide();
            });
        }
    </script>
</head>
<body>
    <h1>Making AJAX call with jQuery 3+</h1>
    <div id="response"></div>
</body>
</html>


and here's the ajax.php:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json');

$data = array(
 array("id"=>1,"name"=>"Brendan","year"=>1992),
 array("id"=>2,"name"=>"Thane","year"=>1997),
 array("id"=>3,"name"=>"Hoyt","year"=>1994),
 array("id"=>4,"name"=>"Xanthus","year"=>1991),
 array("id"=>5,"name"=>"Francis","year"=>1991),
 array("id"=>6,"name"=>"Plato","year"=>1998),
 array("id"=>7,"name"=>"Sylvester","year"=>1998)
);

 $rarray = array(); 
 $rarray['films'] = $data;
 echo json_encode($rarray);

Monday, December 5, 2016

MySQL and JSON column

Since version 5.7 MySQL has support for JSON. Here's create table script and simple CRUD SQL statements.

CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `jdoc` json DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `t1` (`id`, `jdoc`) VALUES
(1, '["officia", "et", "anim", "dolore", "ut", "duis", "quis"]'),
(2, '{"valid_until": "23.03.2016"}'),
(4, '{"valid_until": "24.04.2012"}'),
(6, '["lorem", "ipsum"]'),
(7, '{"valid_until": "24.04.2034"}');

ALTER TABLE `t1`  ADD PRIMARY KEY (`id`);

ALTER TABLE `t1`  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;

-- SELECT id, json_extract(jdoc, "$.valid_until") as jd FROM t1 WHERE json_extract(jdoc, "$.valid_until") IS NOT NULL
-- INSERT INTO t1 VALUES (null, '{"valid_until":"24.04.2034"}' )
-- UPDATE t1 SET jdoc= JSON_SET(jdoc, "$.valid_until", "24.04.2016") WHERE id = 3;
-- DELETE FROM t1  WHERE json_extract(jdoc, "$.valid_until") = '24.04.2016';


Tuesday, July 2, 2013

Parse JSON generated by PHP

This tutorial shows how to parse a JSON string generated by PHP. Place job.php and index.html in same folder.