get请求
function get(){ $.get( "./Aservlet?id=5", function(data, textStatus, jqXHR){ $("body").append( data ); } ); };
发送时候 截取的报文
GET http://localhost:8080/KKserver/Aservlet?id=5 HTTP/1.1 Host: localhost:8080 Connection: keep-alive Accept: */* X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36 Referer: http://localhost:8080/KKserver/test.html Accept-Encoding: gzip, deflate, sdch, br Accept-Language: zh-CN,zh;q=0.8
返回的报文 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Length: 100 Date: Fri, 23 Dec 2016 08:18:11 GMT GET http://localhost:8080/KKserver/Aservlet id=5 GET http://localhost:8080/KKserver/Aservlet id=5
post请求
function post(){ $.post( "./Aservlet?",{ bookId: 2 , money: 100}, function(data,textStatus, jqXHR){ $("body").append( data ); } ); };
发送的报文
POST http://localhost:8080/KKserver/Aservlet? HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 18 Accept: */* Origin: http://localhost:8080 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://localhost:8080/KKserver/test.html Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.8 bookId=2&money=100
返回的报文
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Length: 10 Date: Fri, 23 Dec 2016 08:21:45 GMT POST :ok
post json时候
$.ajax({ url: "./Aservlet", type: "POST", data: ss, success: function(data, textStatus, jqXHR){ $("body").append( data ); } , dataType: "json" });
发送的报文
POST http://localhost:8080/KKserver/Aservlet HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 28 Accept: application/json, text/javascript, */*; q=0.01 Origin: http://localhost:8080 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://localhost:8080/KKserver/test.html Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.8 {"name":"the5fire","age":38}
返回的报文
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Length: 10 Date: Fri, 23 Dec 2016 08:24:03 GMT POST :ok
但是用backbonejs 中的 model的 .save();方法时候
发送的报文
POST http://localhost:8080/KKserver/Aservlet HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 28 Accept: application/json, text/javascript, */*; q=0.01 Origin: http://localhost:8080 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36 Content-Type: application/json Referer: http://localhost:8080/KKserver/demo3.html Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.8 {"name":"张三","age":"38"}
服务端用request.getParameterMap(); 却获取不到数据 不知道这是为什么?
补充 ( chrome 插件 Advanced REST client 挺好的 可以模拟发送http请求)
发现 就是因为
Content-Type: application/json(backbonejs的)
Content-Type: application/x-www-form-urlencoded(常规的)
所以后台常规办法request.getattrivute 是不行的
看文章说 spring 框架可以解决 但是属于后台的范畴我就先不研究了
不知道用servlet有什么好办法呢?如果您看到这里 恰巧知道 请留言谢谢
经过小云同学的努力 知道了
后台可以
//字符串读取 public void charReader(HttpServletRequest request) throws IOException { BufferedReader br = request.getReader(); String str, wholeStr = ""; while((str = br.readLine()) != null){ wholeStr += str; } System.out.println(wholeStr); } //二进制读取 public void binaryReader(HttpServletRequest request) throws IOException { int len = request.getContentLength(); ServletInputStream iii = request.getInputStream(); byte[] buffer = new byte[len]; iii.read(buffer, 0, len); }
偶也!!!!!