dselp3944 2016-09-03 01:12
浏览 64
已采纳

JSON值未正确显示

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.

  • 写回答

3条回答 默认 最新

  • douju6850 2016-09-03 08:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?