get请求
function get(){ $.get( "./Aservlet?id=5", function(data, textStatus, jqXHR){ $("body").append( data ); } ); };
发送时候 截取的报文 GET http://localhost:8080/KKserver/Aservlet?id=5 HTTP/1.1Host: localhost:8080Connection: keep-aliveAccept: */*X-Requested-With: XMLHttpRequestUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36Referer: http://localhost:8080/KKserver/test.htmlAccept-Encoding: gzip, deflate, sdch, brAccept-Language: zh-CN,zh;q=0.8
返回的报文HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Length: 100Date: Fri, 23 Dec 2016 08:18:11 GMTGET http://localhost:8080/KKserver/Aservlet id=5GET 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.1Host: localhost:8080Connection: keep-aliveContent-Length: 18Accept: */*Origin: http://localhost:8080X-Requested-With: XMLHttpRequestUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36Content-Type: application/x-www-form-urlencoded; charset=UTF-8Referer: http://localhost:8080/KKserver/test.htmlAccept-Encoding: gzip, deflate, brAccept-Language: zh-CN,zh;q=0.8bookId=2&money=100
返回的报文
HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Length: 10Date: Fri, 23 Dec 2016 08:21:45 GMTPOST :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.1Host: localhost:8080Connection: keep-aliveContent-Length: 28Accept: application/json, text/javascript, */*; q=0.01Origin: http://localhost:8080X-Requested-With: XMLHttpRequestUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36Content-Type: application/x-www-form-urlencoded; charset=UTF-8Referer: http://localhost:8080/KKserver/test.htmlAccept-Encoding: gzip, deflate, brAccept-Language: zh-CN,zh;q=0.8{"name":"the5fire","age":38}
返回的报文
HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Length: 10Date: Fri, 23 Dec 2016 08:24:03 GMTPOST :ok
但是用backbonejs 中的 model的 .save();方法时候
发送的报文
POST http://localhost:8080/KKserver/Aservlet HTTP/1.1Host: localhost:8080Connection: keep-aliveContent-Length: 28Accept: application/json, text/javascript, */*; q=0.01Origin: http://localhost:8080X-Requested-With: XMLHttpRequestUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36Content-Type: application/jsonReferer: http://localhost:8080/KKserver/demo3.htmlAccept-Encoding: gzip, deflate, brAccept-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); }
偶也!!!!!