本文共 1263 字,大约阅读时间需要 4 分钟。
node 发出ajax请求
I use the term HTTP, but HTTPS is what should be used everywhere, therefore these examples use HTTPS instead of HTTP.
我使用HTTP一词,但是HTTPS是应该在所有地方使用的术语,因此这些示例使用HTTPS代替HTTP。
const https = require('https')const options = { hostname: 'flaviocopes.com', port: 443, path: '/todos', method: 'GET'}const req = https.request(options, (res) => { console.log(`statusCode: ${res.statusCode}`) res.on('data', (d) => { process.stdout.write(d) })})req.on('error', (error) => { console.error(error)})req.end()
const https = require('https')const data = JSON.stringify({ todo: 'Buy the milk'})const options = { hostname: 'flaviocopes.com', port: 443, path: '/todos', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length }}const req = https.request(options, (res) => { console.log(`statusCode: ${res.statusCode}`) res.on('data', (d) => { process.stdout.write(d) })})req.on('error', (error) => { console.error(error)})req.write(data)req.end()
PUT and DELETE requests use the same POST request format, and just change the options.method
value.
PUT和DELETE请求使用相同的POST请求格式,只需更改options.method
值即可。
翻译自:
node 发出ajax请求
转载地址:http://emqgb.baihongyu.com/