electron中请求线上地址在最新版中都用了
- const { BrowserWindow } = require('electron')
- const win = new BrowserWindow({ width: 800, height: 600 })
- win.loadURL('http://github.com').then(() => {
- const currentURL = win.webContents.getURL()
- console.log(currentURL)
- })
这个一般都是get请求,但是如果需要post请求官网也给了示例:
- contents.loadURL(url[, options])
- url string
- options Object (optional)
- httpReferrer (string | Referrer) (optional) - An HTTP Referrer url.
- userAgent string (optional) - A user agent originating the request.
- extraHeaders string (optional) - Extra headers separated by "
".
- postData (UploadRawData | UploadFile)[] (optional)
- baseURLForDataURL string (optional) - Base url (with trailing path separator) for files to be loaded by
- the data url. This is needed only if the specified url is a data url and needs to load other files.
主要用到了 postData 属性
相信很多人用这个属性发送post内容到服务端确发现返回 body{} 字节为空等现象? 别着急看下面代码:
- webview.loadURL(url, {
- postData: [{
- type: "rawData",
- bytes: Buffer.from("foo=bar")
- }],
- extraHeaders: "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"
- });
记住一定要设置: extraHeaders 这就类似ajax发送服务端post请求设置 content-type ,不然就无法发送数据。
|