简体   繁体   English

如何将 PHP json_decode 修改为 jquery.getJSON()?

[英]How to modify a PHP json_decode into a jquery.getJSON()?

I am a newer for study php and jQuery, I tried many times myself, but also not work well.我是学习 php 和 jQuery 的新人,我自己尝试了很多次,但也效果不佳。

How to modify a PHP json_decode into a jQuery.getJSON()?如何将 PHP json_decode 修改为 jQuery.getJSON()? I want modify all the PHPcode into javascript.我想将所有 PHPcode 修改为 javascript。

    $json_data = file_get_contents("data.txt"); 
    $data = json_decode($json_data, true);
    if($data){
    $num = 1;
    foreach ($data as $result) {
    ?>
        $.ajax({
        url: "page2.php", 
        dataType: "html",
        type: 'POST', 
        data: "value=<?php echo $result['name']; ?>",
        success: function(data){ 
            $("#result<?php echo $num; ?>").html(data);
        }
    <?php
    $num++
    }
    }
    ?>

json tree: json 树:

[
 {"name" : "name1"},
 {"name" : "name2"},
 {"name" : "name3"},
]

If you're sure the text file is json then the following should work as JSON is a subset of javascript.如果您确定文本文件是 json,那么以下内容应该可以工作,因为 JSON 是 javascript 的子集。

<?php
$json_data = file_get_contents("data.txt");
echo "var json_data = $json_data;";
?>

for (var i = 0; i<json_data.length; i++) {
        $.ajax({
        url: "page2.php", 
        dataType: "html",
        type: 'POST', 
        data: "value="+json_data[i].name,
        success: function(data){ 
            $("#result"+String(i+1)).html(data);
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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