免费的在线REST API,为前端开发者提供模拟数据服务
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
一个为前端开发者提供的免费在线REST API服务,让开发和测试变得更加简单
"JSONPlaceholder" 这个名字结合了两部分:JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,而Placeholder(占位符)表示它提供的是用于开发和测试的模拟数据,而不是真实的生产数据。
JSONPlaceholder 由 Typicode 团队创建,首次发布于2014年。它的诞生源于开发者在前端开发过程中对模拟API的需求,使得前端开发者可以在后端API尚未准备好的情况下进行开发和测试。
完全免费,无需注册或API密钥
即开即用,无需复杂配置
支持标准的RESTful操作
提供多种资源类型:帖子、评论、用户等
支持CORS,可在任何前端项目中使用
JSONPlaceholder 提供了丰富的模拟API端点,满足各种开发和测试需求
提供模拟的文章数据,包含标题、内容、作者ID等字段
/posts
模拟的评论数据,与帖子相关联,包含邮箱、内容等信息
/comments
模拟的用户数据,包含姓名、邮箱、地址、公司等详细信息
/users
模拟的相册数据,包含标题和所属用户ID
/albums
模拟的照片数据,包含标题、URL和缩略图URL
/photos
模拟的待办事项数据,包含标题和完成状态
/todos
简单几步,即可在你的项目中使用 JSONPlaceholder
JSONPlaceholder 无需注册,直接通过HTTP请求即可使用。基础URL为:
https://jsonplaceholder.typicode.com
你可以通过添加不同的端点来获取不同类型的数据:
获取所有帖子:
GET https://jsonplaceholder.typicode.com/posts
获取ID为1的帖子:
GET https://jsonplaceholder.typicode.com/posts/1
获取ID为1的帖子的所有评论:
GET https://jsonplaceholder.typicode.com/posts/1/comments
// 使用 fetch API 获取帖子
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.error('Error:', error));
// 使用 jQuery 获取帖子
$.get('https://jsonplaceholder.typicode.com/posts/1', function(data) {
console.log(data);
})
.fail(function() {
console.error('Error fetching data');
});
// React 组件中使用
import { useEffect, useState } from 'react';
function Post() {
const [post, setPost] = useState(null);
useEffect(() => {
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(res => res.json())
.then(data => setPost(data));
}, []);
return post && (
{post.title}
{post.body}
);
}
// 提交新帖子(模拟)
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: '新帖子标题',
body: '帖子内容',
userId: 1
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then(response => response.json())
.then(json => console.log(json));
来自全球开发者的真实评价