博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ajax之 get post请求
阅读量:6048 次
发布时间:2019-06-20

本文共 3988 字,大约阅读时间需要 13 分钟。

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);    }
 

偶也!!!!!

 

 

转载于:https://www.cnblogs.com/WhiteHorseIsNotHorse/p/6215264.html

你可能感兴趣的文章
Oracle的层次查询
查看>>
远程调用服务(RPC)和消息(Message Queue)对比及其适用/不适用场合
查看>>
FreeBSD 的 Ports 系统
查看>>
有关web
查看>>
读Nginx官方文档笔记
查看>>
Spring中用了哪些设计模式?
查看>>
存储问题
查看>>
转: jquery中ajax回调函数使用this
查看>>
我的Java开发学习之旅------>Java 格式化类(java.util.Formatter)基本用法
查看>>
我的Android进阶之旅------>adbd cannot run as root in production builds 的解决方法
查看>>
Nginx http升级到https
查看>>
关于在RK3288上安装Opencv的方法
查看>>
7.Java集合-Arrays类实现原理及源码分析
查看>>
[POI2008]Triangles
查看>>
2016开发一个app需要多少钱?app开发需要哪些成本-app开发问题汇总-广州达到信息...
查看>>
程序找不到jvm的解决方法
查看>>
Java中Volatile的理解
查看>>
c++primer page 249 answer
查看>>
04单例模式Singleton
查看>>
SSE图像算法优化系列六:OpenCv关于灰度积分图的SSE代码学习和改进。
查看>>