远程获取数据 开发环境有跨域问题 浏览器 前端开发服务器 后端测试服务器 http://localhost:8080/ 页面 ajax 跨域:http://test-data:3000/api/news JSON数据 浏览器阻止数据移交 浏览器 前端开发服务器 后端测试服务器 生产环境没有跨域问题 浏览器 服务器 http://www.my-site.com/ 页面 ajax:http://www.my-site.com/api/news JSON数据 浏览器 服务器 浏览器 静态资源服务器 数据服务器 http://www.my-site.com/ 页面 ajax 跨域:http://api.my-site.com/api/news [允许www.my-site.com]JSON数据 浏览器 静态资源服务器 数据服务器 解决开发环境的跨域问题 浏览器 前端开发服务器 后端测试服务器 http://localhost:8080/ 页面 ajax:http://localhost:8080/api/news 代理请求:http://test-data:3000/api/news JSON数据 JSON数据 浏览器 前端开发服务器 后端测试服务器 为什么要Mock数据 浏览器 前端开发服务器 后端测试服务器 http://localhost:8080/ 页面 ajax:http://localhost:8080/api/news 代理请求:http://test-data:3000/api/news 404 (后端正在开发中) 404 浏览器 前端开发服务器 后端测试服务器 浏览器 MockJS 前端开发服务器 定义ajax拦截规则 http://localhost:8080/ 页面 ajax:http://localhost:8080/api/news 模拟的JSON数据 浏览器 MockJS 前端开发服务器