ajax json response code example

Example 1: jquery ajax json

$.ajax({
  method: "POST",
  url: "some.php",
  dataType: "json",
  data: {}
}).done(json => console.log(json));

Example 2: how to set json type jquery ajax

$.ajax({
            type: "POST",
            url: siteRoot + "api/SpaceGame/AddPlayer",
            async: false,
            data: JSON.stringify({ Name: playersShip.name, Credits: playersShip.credits }),
            contentType: "application/json",
            complete: function (data) {
            console.log(data);
            wait = false;
        }
    });

Example 3: php json response to ajax

<?php
 echo json_encode($data);
?>

Tags:

Php Example