前后端分离时遇到的坑
(1) 当请求type错误时,会返回服务器会返回一部分不认识的HTML,导致不能解析返回结果进行下一步操作,这是由于请求头的原因,请求头里面需要设置Content-Type:application/x-www-form-urlencoded,所以做web开发的时候,http的各种请求头的含义是什么,一定要搞清楚噢,所以这里记录一下http的请求头(百度上有):
1. HTTP请求头
accept:浏览器通过这个头告诉服务器,它所支持的数据类型。如:text/html, image/jpeg
accept-Charset:浏览器通过这个头告诉服务器,它支持哪种字符集。
accept-encoding:浏览器通过这个头告诉服务器,它支持哪种压缩格式。
accept-language:浏览器通过这个头告诉服务器,它的语言环境。
host:浏览器通过这个头告诉服务器,它想访问哪台主机。
if-modified-since:浏览器通过这个头告诉服务器,缓存数据的时间
referer:浏览器通过这个头告诉服务器,客户机是哪个页面来的(防盗链)。
Connection:浏览器通过这个头告诉服务器,请求完后是断开链接还是维持链接。
2. HTTP响应头
location:服务器通过这个头告诉浏览器跳到哪里。
server:服务器通过这个头告诉浏览器服务器的型号。
content-encoding:服务器通过这个头告诉浏览器数据的压缩格式。
content-length:服务器通过这个头告诉浏览器回送数据的长度。
content-language:服务器通过这个头告诉浏览器语言环境。
content-type:服务器通过这个头告诉浏览器回送数据的类型。
refresh:服务器通过这个头告诉浏览器定时刷新。
content-disposition:服务器通过这个头告诉浏览器以下载方式打开数据。
transfer-encoding:服务器通过这个头告诉浏览器数据是以分块方式回送的
以下三个表示服务器通过这个头告诉浏览器不要缓存
expires:-1
cache-control:no-cache
pragma:no-cache
3. 响应头在开发中的示例
public class ServletDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // locationTest(resp); // zipTest(resp); contentTypeTest(resp); // refreshTest(resp); } //指定浏览器定时刷新 private void refreshTest(HttpServletResponse resp) throws IOException { // resp.setHeader("refresh", "3");//3秒钟刷新一次 resp.setHeader("refresh", "3;url='http://www.baidu.com'");//隔三秒跳转到百度 resp.getWriter().write("java web"); } //指定回送数据类型以及以下载方式呈现到浏览器 private void contentTypeTest(HttpServletResponse resp) throws IOException { resp.setHeader("Content-type", "image/jpeg");//关于类型的value值,可以在tomcat目录下的\config\web.xml中找到 resp.setHeader("Content-Disposition", "attachment;filename=dog.jpg");//告诉浏览器以下载的方式呈现给用户 InputStream in = this.getServletContext().getResourceAsStream("/dog.jpg"); //读写模板代码 byte buffer[] = new byte[1024]; int len = 0; OutputStream out = resp.getOutputStream();//拿到响应的输出流 while((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } //数据压缩 private void zipTest(HttpServletResponse resp) throws IOException { String data = "java web学习 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; System.out.println("原始数据的大小:" + data.getBytes().length); //获得一个输出流,其中的数据被写入一个byte数组,缓冲区会随着数据的不断写入而自动增长 //可使用toByteArray()和toString()获得数据 ByteArrayOutputStream bout = new ByteArrayOutputStream(); GZIPOutputStream gout = new GZIPOutputStream(bout);//获得一个压缩流,参数为一个输出流,这个输出流捕获压缩后的数据并将其写到浏览器 gout.write(data.getBytes());//调用压缩流的write方法即可将指定数据进行压缩,参数为字节流数据 gout.close();//将缓冲数据写到流中 //获得压缩后的数据 byte g[] = bout.toByteArray(); System.out.println("压缩数据的大小:" + g.length); resp.setHeader("Content-Encoding", "gzip");//通知浏览器这是压缩数据 resp.setHeader("Content-Length", g.length + "");//并告诉浏览器压缩数据的长度 resp.getOutputStream().write(g);//打印到浏览器中 } //location头的应用 private void locationTest(HttpServletResponse resp) { resp.setStatus(302);// resp.setHeader("location", "/day04/MyHtml.html");//服务器通过location告诉浏览器跳转到MyHtml.html页面执行 } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
(2) 注意跨域问题,可以直接再后端解决掉,前端会一直报403错误
(3) 前后端调接口的时候,一定要在postman请求成功了以后再写代码