简体   繁体   中英

How to append JSON looking string to HTML pre tag?

I have a part of page looking like this:

在此输入图像描述

And here is how it should look: 在此输入图像描述

Now I'm retrieving from database all data that I need to show on page.

$korisnickiId=$_POST['userId'];
$q="SELECT `facebook_app_id`,`facebook_ids`,`authorization_date`,`status`,`date_added`,`users_added` FROM `previous_imports` WHERE `user_id`='$korisnickiId'";
$result=$mysqli->query($q);
while($fetchedResult=$result->fetch_assoc()) {
$encoded = $fetchedResult;
$makeItApliable='{';
foreach($encoded as $key=>$value){
$makeItApliable.='
"'.$key.'" : "'.$value.'",
';
}
$makeItApliable.='},';
rtrim($makeItApliable, ",");
echo ($makeItApliable);
}

So $makeItApliable after this chunk of code looks like this: 在此输入图像描述 And I want it to look exactly like this on page, so I'm echoing $makeItApliable variable and running this part of jQuery code(everything is done via Ajax ):

$.ajax({
      url: "../includes/adapter.php",
      type: "POST",
      dataType: "JSON",
      data: data,
      async: true,
      success: function (data) {
      if(data){
      console.log(data);
      $('#goliSpan').html("<span>"+data+"</span>");
               }
      }
 });

And this is HTML where this data should be appended:

<pre id="istosakriti" class="">
HTTP 200 OK
Allow: <span class="plavi-txt">GET, POST</span>
Content-Type: <span class="plavi-txt">application/json</span>
Vary: <span class="plavi-txt">Accept</span>
        <span id="goliSpan" class="tamnocrveno"></span>
</pre>

So in this span whose id is goliSpan this JSON(or JSON looking string) should be appended. But When I run code above nothing happens. Even tried:

$('#goliSpan').append("<span>"+data+"</span>");

And

$('#goliSpan').text(data);

But nothing works. If you have any idea, please help.

use json_encode to encode $result and set Content-Type , update your PHP code:

$korisnickiId=$_POST['userId'];
$q="SELECT `facebook_app_id`,`facebook_ids`,`authorization_date`,`status`,`date_added`,`users_added` FROM `previous_imports` WHERE `user_id`='$korisnickiId'";
$result=$mysqli->query($q);

header('Content-Type: application/json');
echo json_encode($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM