http://nodejs.cn/api/http.html#http_http_request_options_callback
http://yijiebuyi.com/blog/8221eb14c8482e7efd1868946e99ea7c.html
1、源生 http.request 模块的做法
http.request({ method: 'POST', url: 'http://192.168.0.102', headers: { // 'Content-Type': 'application/json', // 'X-Requested-With': 'XMLHttpRequest', }, // 请求和回发的数据自动转变成了 json 对象 // 不需要在header中设置'Content-Type': 'application/json',也不需要手动JSON.stringify()转义Body postdata json: true, body: { 'Phone': mobile, 'Code': code, 'Pwd': pwd, 'Share': share }, }, function (err, response, body) { console.log(body); })
2、request模块(推荐)
request({ method: 'POST', url: 'http://192.168.0.102', headers: { // 'Content-Type': 'application/json', // 'X-Requested-With': 'XMLHttpRequest', }, // 请求和回发的数据自动转变成了 json 对象 // 不需要在header中设置'Content-Type': 'application/json',也不需要手动JSON.stringify()转义Body postdata json: true, body: { 'Phone': mobile, 'Code': code, 'Pwd': pwd, 'Share': share }, }, function (err, response, body) { console.log(body); })
本文介绍了使用Node.js进行HTTP请求的两种方法:一是利用原生http.request模块;二是使用request模块。这两种方法均支持自动将请求和响应数据转换为JSON对象,无需手动设置Content-Type或进行JSON字符串化。
330

被折叠的 条评论
为什么被折叠?



