gpt4 book ai didi

javascript - Ajax 使用 jquery 获取对 json 文件的请求

转载 作者:行者123 更新时间:2023-11-29 23:44:14 25 4
gpt4 key购买 nike

我是 jQuery 和 Ajax 的新手,我正在尝试使用 jQuery 和 ajax 来请求 JSON 文件。这是结构。

[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
}
]

我想把它放入一个对象数组中。我的代码是这样的

 $("button").click(function(){
$.get("https://jsonplaceholder.typicode.com/todos", function(data, status){
array=JSON.parse(data);
join();
render();

});
});

那行不通,但这个行

 var xhttp= new XMLHttpRequest();
xhttp.onreadystatechange=function()
{
//console.log(this.readyState+" "+this.status);
if(this.readyState==4&&this.status==200)
{
array=JSON.parse(this.response);

join();
render();


}
}
xhttp.open("GET","https://jsonplaceholder.typicode.com/todos",true);
xhttp.send();

最佳答案

使用 $.getJSON() 而不是 $.get() ,如下所示:-

$.getJSON( "https://jsonplaceholder.typicode.com/todos", function( data ) {

console.log(data);//check data coming properly or not

//do rest of the coding accordingly
});

关于javascript - Ajax 使用 jquery 获取对 json 文件的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com