KAz KAz - 3 months ago 14
JSON Question

JSON values not showing properly

I wrote a php script which accept POST request from ajax and give the response back. All working fine. But the receiving string split letter by letter I can't understand what is the reason.

Here is my AJAX code,

$("#btn").click(function(){
console.log($("#search_bar").val());
var dataV;
var htmlText = '';
var containerbootsrap = '';
var filename = '';
var index_no;
$.ajax({
type: "POST",
crossDomain: true,
url: "http://localhost:8090/ontology/setText",
data: $("#search_bar").val(),
contentType: 'text/plain',
// dataType: "json",
success: function( data, textStatus, jQxhr ){
console.log('data');
console.log(data);

for( var item in data) {
console.log ("item: " + item);
console.log ("data: " + data[item]);
index_no = data[item];

// htmlText += '<div class="div-conatiner">';
// htmlText += '<p class="p-name"> Name: ' + data[item] + '</p>';
// htmlText += '<img class="imageload" src="' + data[item] + '" />';
// htmlText += '</div>';

// filename = data[item].replace(/^.*[\\\/]/, '')

$.ajax({
data: 'index_no=' + index_no,
url: 'retrivedata.php',
method: 'POST', // or GET
dataType: 'json',
success: function(msg) {
console.log(msg);
for(var item in msg){
console.log ("item: " + item);
console.log ("data: " + msg[item]);
}

$('#home').hide();

containerbootsrap += '<div class = "container" id="search_container">';
containerbootsrap += '<div class = "row homepage">';
containerbootsrap += '<div class = "col-md-5 col-md-offset-3">';
containerbootsrap += '<a href="#" class="thumbnail">';
containerbootsrap += '<img class="imageload" src="' + msg + '" />';
containerbootsrap += '<h3 id="video_name"> ' + filename + ' </h3>'
containerbootsrap += '</a>';
containerbootsrap += '</div>';
containerbootsrap += '</div>';
containerbootsrap += '</div>';

$('body').append(containerbootsrap);

}
});

// $.post('retrivedata.php', { num: 5 }, function(result) {
// alert(result);
// });

// $('#home').hide();
}

// $('body').append(containerbootsrap);

},
error: function( jqXhr, textStatus, errorThrown ){
console.log( jqXhr );
alert(jqXhr)
}

});
});


php code is below

<?php
$index_no = $_POST["index_no"];
// echo $index_no * 2;

include('dbConnection.php');
$query = mysql_query("SELECT * FROM video_data WHERE index_no = $index_no");

while ($row = mysql_fetch_assoc($query)) {
$imagePath = $row['thumbnail_url'];
$videoPath = $row['video_url'];
// echo $imagePath;
// echo $videoPath;
echo json_encode($imagePath);
}

?>


I need the output as : 'imagepath'
but it is giving the output as split letter by letter.

here is the real output

Output

but i need the output in one line. like /video_frames/bb/frame136.jpg
please help me to figure out where I am going wrong.

Answer

Well, in the php code where you're returning the value you need to specify an array not an string. The variable there $imagePath seems to be a string. You can do something like this.

echo json_encode(array('result' => $imagePath));

This will give you your result in the 'result' key. You can parse it and use it.

Comments