从URL使用AJAX显示JSON数据数据、URL、AJAX、JSON

2023-09-11 22:35:22 作者:总有刁民想害朕

我们需要显示我们的JSON文件的所有名字到使用javascript一个div。我们尝试了很多东西,但我们没能成功。

JSON数据: http://www.smartbustracking.be/json/data.json

这是我们尝试:

 <按钮>获取数据和LT; /按钮>
< D​​IV ID =resultJson>< / DIV>


<脚本类型=文/ JavaScript的LANGUAGE =JavaScript的>
             $(按钮)。点击(函数(){
                    $ .getJSON(http://www.smartbustracking.be/json/data.json功能(结果){
                        为(变种X = 0 X  - 其中; result.length; X ++){

                                $每个(结果,功能(我,场){
                                    $(#resultJson)追加(场[X]。名称)。
                                }

                        }
                    });
            });
 
jquery ajax 向后台传 json 数组 出现 400 或 415 错误,后台用的springmvc

如果任何人都可以帮助我们完成这一ploblem,那将是巨大的。

在此先感谢!

解决方案

不使用额外的循环中循环提取的名字。

您的数据就像

  [0] = {名NAME1}
[1] = {名字:NAME2}
 

如果里面有一个数组[0],你想,那么你需要另外一个循环内循环来提取值就像你想拉 bushaltes 。

试试这样

$(按钮)。点击(函数(){     $ .getJSON(http://www.smartbustracking.be/json/data.json功能(结果){         执行console.log(结果);         为(变种X = 0 X - 其中; result.length; X ++){             $(#resultJson)追加(结果[X]。名称)。         }     }); });

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/jquery/2.1.1/jquery.min.js"></script> &LT;按钮&GT;获取数据和LT; /按钮&GT; &LT; D​​IV ID =resultJson&GT;&LT; / DIV&GT;

We want to display all the names from our json file into a div using javascript. We tried a lot of things, but we didn't manage to succeed.

json data : http://www.smartbustracking.be/json/data.json

This is what we tried :

<button>get data</button>
<div id="resultJson"></div>


<script type="text/javascript" language="javascript">
             $("button").click(function(){
                    $.getJSON("http://www.smartbustracking.be/json/data.json", function(result){
                        for(var x = 0; x < result.length; x++){

                                $.each(result, function(i, field) {
                                    $("#resultJson").append(field[x].name);
                                }

                        }
                    });
            });

If anyone can help us with this ploblem, that would be great

Thanks in advance !

解决方案

don't use extra loop inside for loop to extract name.

Your data is like

[0] = {name:"name1"}
[1] = { name: "name2"}

If there is a array inside [0] and you want to extract value from that then you need another loop inside for loop to extract that value like you wanna to pull value of bushaltes.

Try like this

$("button").click(function() {
    $.getJSON("http://www.smartbustracking.be/json/data.json", function(result) {
        console.log(result);
        for (var x = 0; x < result.length; x++) {
            $("#resultJson").append(result[x].name);
        }
    });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>get data</button>
<div id="resultJson"></div>