ajax的post方法相比get方法,在传参形式上很不一样, get把参数用'?'拼接在端口后,并且用'&'连接;而post则是需要在send参数里设置.

根据ajax实例xhr.setRequestHeader('content-type', )中第二个参数的不同,  send的参数也不相同.

最常用的有两种: application/x-www-form-encoded 和 application/json两种形式.

1
       const username = document.getElementById('username').value,
       password = document.getElementById("password").value;
     var xhr = new XMLHttpRequest();
xhr.open('POST','/test');
// xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
// xhr.send(`name=${username}&&password=${password}`);
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(JSON.stringify({username, password}))

express本身只能用get方法,对用post方法的请求, 没法查看request的啥. 所以用第三方插件body-parser;

 const bodyParser = require('body-parser');
const app = express();
// var urlencodedParser = bodyParser.urlencoded({ extended: false });
// app.post('/test',urlencodedParser,(req,res)=>{
// console.log(req.body)
// }) var jsonParser = bodyParser.json();
app.post('/test',jsonParser,(req,res)=>{
console.log(req.body)
})

ajax 的post方法 的content-type设置和express里应用body-parser的更多相关文章

  1. ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported错误

    ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedE ...

  2. jmeter报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"的解决方法

    1.报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supporte ...

  3. ajax的使用:(ajaxReturn[ajax的返回方法]),(eval返回字符串);分页;第三方类(page.class.php)如何载入;自动加载函数库(functions);session如何防止跳过登录访问(构造函数说明)

    一.ajax例子:ajaxReturn("ok","eval")->thinkphp中ajax的返回值的方法,返回参数为ok,返回类型为eval(字符串) ...

  4. 011_URL和Ajax辅助器方法

    创建基本的链接和URL 在我们介绍链接或URL之前先做一些准备,我们这部分要介绍的知识将要使用的项目就是之前建立的HelperMethods项目,现在需要先为其添加一个People控制器,并在其中定义 ...

  5. Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...

  6. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  7. 转载 SharePoint【Site Definition 系列】– 创建Content Type

    转载原地址:  http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...

  8. the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

    the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...

  9. Ajax的load方法演示

    load方法的参数形式为: load(url,[data],[callback]); 其中url为请求HTML页面的URL地址.[data]表示发送至服务器的key/value数据.callback表 ...

随机推荐

  1. jenkins 邮箱通知设置

    https://blog.csdn.net/boonya/article/details/77335074 https://blog.csdn.net/lovedingd/article/detail ...

  2. eclipse设置maven web项目打包

    如图:eclipse下的maven web项目,打包部署到本地tomcat时,需要关注的2个方面: 1. src/main/webapp目录下的文件,打包到/ 根路径下 2. 添加maven 依赖,打 ...

  3. javascript 通用定义

    通用约定 注释 原则 As short as possible(如无必要,勿增注释):尽量提高代码本身的清晰性.可读性. As long as necessary(如有必要,尽量详尽):合理的注释.空 ...

  4. Bugku 杂项 又一张图片,还单纯吗

    又一张图片,还单纯吗 下载后,用binwalk打开图片 使用foremost 2.png进行分离 得到图片 关于foremost foremost [-v|-V|-h|-T|-Q|-q|-a|-w-d ...

  5. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...

  6. [ZJU 1004] Anagrams by Stack

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  7. WEB Fuzz中需要关注的7种响应

    WEB应用模糊测试(WEB Fuzz)是一种特殊形式的网络协议模糊测试,专门关注遵循HTTP规范的网络数据包. WEB Fuzz并不是新的概念,目前有多种WEB应用模糊测试器(WEB Fuzzer), ...

  8. iOS-7-Cookbook

    https://github.com/liubin1777/iOS-7-Cookbook 版权声明:本文为博主原创文章,未经博主允许不得转载.

  9. ctrl+r 调用bash曾经的历史命令

    在bash界面 按ctrl+r 可以调出, bash中曾经的历史命令, 光标会停留在 第一次被匹配的字符上, (即使后面你再输入被匹配的字符, 光标也不移动) 然后, 根据你的需要 来进行任何一次的操 ...

  10. linux 下spyder安装

    linux  下spyder安装: 安装qt4,python3 对应qt5 sudo apt-get install libxext6 libxext-dev libqt4-dev libqt4-gu ...