webview js ajax请求头,javascript - Ajax's headers not being sent (via electron webview) - Stack Overflo...

I have tried the solutions that I found here for the headers issue, e.g. beforeSend and checking the version of ajax. I basically have an electron app, inside, there's a webview, this webview communicates with an ipc script, this ipc script adds jquery to the visited page, then it executes an ajax request. I'm using Vue for the front-end.

The problem is, it's not sending the custom requests that I'm putting. Although, It was working 100% before. I really don't remember what caused it.

The main index.js of my electron app

mainWindow = new BrowserWindow({

height: 850,

useContentSize: true,

width: 1550,

webPreferences: {

webSecurity: false,

nodeIntegration: true,

webviewTag: true

}

})

The webview

the ipc.js file

const {

ipcRenderer

} = require('electron');

const _ = require("lodash")

const myajaxfile = require("./myajaxfile.js")

window.onload = function() {

var script = document.createElement("script");

script.src = "https://code.jquery.com/jquery-3.5.1.min.js";

script.onload = script.onreadystatechange = function() {

};

document.body.appendChild(script);

// var script2 = document.createElement("script");

// script2.src = "https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js";

// script2.onload = script2.onreadystatechange = function() {

// };

// document.body.appendChild(script2);

};

ipcRenderer.on("get_item", function(event, payload) {

myajaxfile.sendRequest()

});

the myajaxfile.js ajax part (basic template):

let stream_ajax = $.ajax({

method: "GET",

url: `https://example.com/ajaxCenter?_action=getserver}`,

headers: {

"Access-Control-Allow-Origin": "true",

'accept': '*/*',

'X-Requested-With': 'XMLHttpRequest',

'x-csrf-token': csrf_token

},

complete(response, status) {

if (status === "success") {

} else {

}

}

});

Nothing of the headers is being sent. Before, it was working 100% well without any issues. Any help is really appreciated, this issue is halting my whole project.

I have uploaded a sample of the project, which could be downloaded here. install the packages with npm install and then npm run watch to run the electron app. After running it, please click on the button connect and check the network tab in the console.

The provided request in the project works only if the csrf-token was sent in the headers, otherwise, it would cause a redirect. Previously, it used to work without any issues.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值