Javascript 使用jquery获取json文件的Ajax请求

Javascript 使用jquery获取json文件的Ajax请求,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我不熟悉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":

我不熟悉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();

这将使用JSON中的对象填充MyObject数组

let myObjects = [];    
$("button").click(function(){
        $.get("https://jsonplaceholder.typicode.com/todos", function(data, status){
        array=JSON.parse(data);
        if(response)
        {
        data.forEach(function (obj) {
           myObjects.push(obj)
        })

           join();
           render();
        }
        else {
           response=true;
        }
    });
});
使用
$.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
});

此处无需检查
响应
,原因有二:

  • $未传递具有此名称的参数。get
  • 代码所在的函数是一个
    success
    回调函数。因此,您肯定会有一个成功的响应,并且您的数据是存在的
  • 因此,您可以这样编写代码:

    $("button").click(function(){
        $.get("https://jsonplaceholder.typicode.com/todos", function(data, status){
            array=JSON.parse(data);
            join();
            render();
        });
    });
    
    如果数据已经在
    JSON
    中,则不需要调用
    JSON.parse

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

    问题是什么?问题是什么?我想不需要
    JSON.parse!只需执行
    array=data
    问题出在哪里?你所说的
    不工作是什么意思?
    join
    render
    的代码是什么?JQuery版本最小值?