weixin_33670713 2017-07-26 01:12 采纳率: 0%
浏览 23

将复杂的文本传递给JQuery

I have ran into a strange behavior of passing JSON from PHP to jQuery.

I have some articles (Drupal CMS articles) and I need to push them into the environment. First solution worked, while the articles was pushed directly by PHP on page load. But as my work works with more than one CMS, it takes too much time to load them all, so I need to use ajax to load one specific project at time.

now, jquery looks like this:

function doLoadArticles() {

for (var i = 0; i< projectdata[1].length; i++){
 $.ajax({
         data: {ip:projectdata[0]["ip"], login:projectdata[0]["login"], pass:projectdata[0]["pass"], db:projectdata[0]["db"], datatype:projectdata[1][i], aj:"aj", fc:"doLoadArticles"},
         type: "post",
         url: "dataFunnel.php",
         success: function(data){
           console.log(data);
           //console.log(jQuery.parseJSON( data )) ;

         }


         });  

}

}

Then, on PHP side is this code:

function doLoadArticles(){

   $projdata;

  $conn = new mysqli($_POST['ip'], $_POST['login'], $_POST['pass'], $_POST['db']);
     // Check connection
     if ($conn->connect_error) {
     die("<div style='position:absolute; top:0;left:0; background:white;'>Connection failed: " . $conn->connect_error." <br><br>Plese try reload the page</div>");
     break;
     }
    else{}


     $sql = "Select node_revision.title, node_revision.nid, node.language, field_revision_body.body_value, field_revision_body.bundle, node_revision.timestamp  
      from field_revision_body 
      LEFT JOIN node_revision ON field_revision_body.revision_id=node_revision.vid 
      LEFT JOIN node ON field_revision_body.revision_id=node.vid 
      where field_revision_body.bundle='".$_POST['datatype']."' AND node_revision.status=1 AND node.language='cs' 
      ORDER BY field_revision_body.revision_id DESC LIMIT 10";
      $result = $conn->query($sql);

       if ($result->num_rows > 0) {
          // error_log("true"); 
         // output data of each row
            $j =0;
          while($row_a = $result->fetch_assoc()) {    


        $projdata[$j]["title"] = $row_a["title"] ;           
        $projdata[$j]["timestamp"] = $row_a["timestamp"] ;
        $projdata[$j]["bundle"] = $row_a["bundle"] ;
        $projdata[$j]["body_value"] = $row_a["body_value"];
        $projdata[$j]["nid"] = $row_a["nid"];
        $projdata[$j]["language"] = $row_a["language"];  

            $j++;

            }
      }

      echo json_encode($projdata);


}

The problem is, that the data are there, only the passing itself does not work .. (probably because of some buged conversion?)

if I do

echo print_r($projdata);

enter image description here it will pass the data, but not in usable way (ignore the broken chars, that is another not related problem, the data are thare, that is what matters)

However, as it is clear, I need it in form that I can work with, so I need json. But if I use

echo json_encode($projdata);

it will pass some "broken nothing" enter image description here

So is there any my mistake I am not aware of, or it is really some kind of bug in PHP implementation? (by the way, I run on PHP 5.6 on MS IIS7)

Or any other way possible, how to load data dynamically without the json conversion?

Thanks in advance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 如何批量获取json的url
    • ¥15 对法兰连接元件所承受的表面载荷等效转化为法兰开孔接触面上的等效表面载荷?
    • ¥15 comsol仿真压阻传感器
    • ¥15 Python线性规划函数optimize.linprog求解为整数
    • ¥15 llama3中文版微调
    • ¥15 pg数据库导入数据序列重复
    • ¥15 三分类机器学习模型可视化分析
    • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
    • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
    • ¥30 python,LLM 文本提炼