如何修复“未定义”的get方法?

时间:2019-06-29 10:48:20

标签: javascript jquery html json methods

我正在从本地计算机使用伪造的json并将其托管在json服务器上。这是我的CLI ...

GET /posts 200 19.549 ms - 77
GET /posts 304 15.604 ms - -
GET /posts 304 6.905 ms - -
GET /posts 304 8.686 ms - -
GET /posts 304 11.422 ms - -
GET /posts 304 5.440 ms - -
GET /posts 304 6.910 ms - -
GET /posts 304 6.818 ms - -

当我在chrome中测试我的网址(第一个请求)成功并显示...

"id": 1,
"title": "json-server"
"author": "typicode"

当我尝试在Js小提琴的代码段中执行请求时,收到:

ID: Undefined 
Title: Undefined 
Author: Undefined 

为什么会这样?

<button>Search</button>



$(document).ready(function(){
$("button").click(function(){
$.get("http://localhost:3000/posts", function(id, title, author){
alert("ID: " + id.id + "\nTitle: " + title.title + "\nAuthor: " + 
author.author);
});
});
});

0 个答案:

没有答案