0

I need help formatting json return in html from my jquery json result.

this my html code :

$("document").ready(function(){
  $(".js-ajax-php-json").submit(function(){
    var data = {
      "action": "testerer"
    };
    data = $(this).serialize() + "&" + $.param(data);
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "test.php",
      data: data,
      success: function(data) {
        $(".the-return").html(data["json"]);
      }
    });
    return false;
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<form action="test.php" class="js-ajax-php-json" method="post" accept-charset="utf-8">
  <input type="text" name="testerer" value="" placeholder="Favorite restaurant" />
  <input type="submit" name="submit" value="Submit form"  />
</form>
<div class="the-return"></div>

and this my test.php

if (is_ajax()) {
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $_POST["action"];
    switch($action) { //Switch case for value of action
      case "testerer": testerer(); break;
    }
  }
}

//Function to check if the request is an AJAX request
function is_ajax() {
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function udemy(){

$courseid  = $_POST['testerer'];
$client_id     = 'xxx';
$client_secret = 'xxxxx';

// set HTTP header
$headers = array(
    "Authorization: Basic " . base64_encode($client_id . ":" . $client_secret),
    "Content-Type: application/vnd.api+json"
);

$url = 'https://xxx/' . $courseid . '/';

// Open connection
$ch = curl_init();

// Set the url, number of GET vars, GET data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Execute request
$result = curl_exec($ch);

// Close connection
$data["json"] = json_encode($result);
  echo json_encode($data, JSON_PRETTY_PRINT);
}

always got return like this :

"{\"_class\": \"course\", \"id\": 5816, \"title\": \"XXx xxx xxx\", \"url\": \"\/xxx-xxx\/\",

I need result readable.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Nixoza
  • 1

2 Answers2

0

It's not clear what your $data should contain, but it looks like the problem is that you're calling json_encode() twice on the same data:

$data["json"] = json_encode($result);
echo json_encode($data, JSON_PRETTY_PRINT);
embden
  • 161
  • 9
  • already remove encode but still got "{"_class": "course", "id": 5816, "title": "Xxx xxx"" still not formated.. only remove \ – Nixoza Apr 20 '15 at 03:02
  • yes.. that jason.. but not formarter or unstructure.. i want something like this one https://gist.github.com/bahaddinyasar/f98a2b0b21aab4e2cb55 – Nixoza Apr 20 '15 at 12:57
0

Convert JSON data into string

JSON.stringify(data["json"])

add as below

$(".the-return").html(JSON.stringify(data["json"]));
Rahaman
  • 323
  • 1
  • 9