4

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

<webview :id="webview.key" :src="webview.url" :preload="fullpath + `\\serverBrowserIPC.js`" style="height: 100%" pcontextIsolation></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.

Jaeger
  • 1,646
  • 8
  • 27
  • 59
  • Have you inspected the network request? Did the browser send the headers? Maybe it's just a caching issue? – Sally CJ Jul 25 '20 at 03:26
  • I did, the browser doesn't send any headers. Not even the `accept` or anything else. – Jaeger Jul 29 '20 at 13:36
  • @Jaeger actually the headers are being sent and you can see that at the CLI console (*not electron chrome console*), see screenshot here: http://prntscr.com/trzx0e Also in the source code inside the zip file, there is no `ipc.js` file neither any `` component anywhere. – Christos Lytras Jul 31 '20 at 20:51
  • I didn't include those files to keep it simple. The project itself has a lot of files that are not required. I'm seeing this too in the console, but stopped showing in the electron chrome console. – Jaeger Aug 01 '20 at 18:16

3 Answers3

1

Since you're sending custom headers this is governed by CORS and will happen in two steps. It's best illustrated in another StackOverflow answer here.

The biggest issue I see is that you're trying to send the server Access-Control-Allow-Origin when that is a header that should be coming from the server in the response. I'd double check that the server you're connecting to is configured for CORS. You can also try logging your error messages. The jqxhr returned by $.ajax has quite a few ways to get at the status text. It should help narrow down exactly where your call is failing.

James Tomasino
  • 3,520
  • 1
  • 20
  • 38
  • The request is returning a `301` status and then redirects to a different location. It's happening because the headers are not set right. The params are right and the request was tested via `Postman` and it works well. It only gets a `301` using the code. – Jaeger Jul 26 '20 at 17:37
  • Hello, I've updated the OP with a provided sample. Could you please check it? – Jaeger Jul 29 '20 at 13:39
  • There's a small js error in the index.js and I had to create an empty store.js in the /src/renderer/ folder to get the project to run. Even with that, I'm getting errors and can't get to your issue. – James Tomasino Jul 29 '20 at 13:58
  • That's weird. I'll check the project and upload it with the packages, I'll update you. Thank you for letting me know! – Jaeger Jul 29 '20 at 14:44
0

Really strange, that is the way to send headers in ajax calls through jquery.

Are you really sure about your issue? If yes, the problem probably is somewhere else, not in the reported code.

Once said that, you could remove two of your custom headers: X-Requested-With it is sent by default with value XMLHttpRequest by jquery and Access-Control-Allow-Origin as the protocol expects to find it in server response (not in client request).

Daniele Ricci
  • 15,422
  • 1
  • 27
  • 55
  • I'm sure the problem isn't with the code. The problem itself is related maybe to `electron` wrapper, because I was sending requests before and it was working well. I have removed the server-side headers and It didn't change anything. – Jaeger Jul 26 '20 at 17:38
  • Hello, I've updated the OP with a provided sample. Could you please check it? – Jaeger Jul 29 '20 at 13:39
0

There is nothing wrong in your client-side(electron), The CORS (Cross-Origin Resource Sharing) needs to be configured on your server-side. The Access-Control-Allow-Origin header is supposed to be sent by the server, not the client(Your Ajax Call.). You have to have CORS configured in your server to enable the cross-origin request otherwise clients like Axios, jquery ajax won't work. Ask your server admin or application developer for it. Here is ho it's work

cors

when the server responds with Access-Control-Allow-Origin: *, which means that the resource can be accessed by any domain. YOu can also go for Preflight Request approach, by sending the HTTP OPTIONS method to the server before calling the actual endpoint. Here is the MDN docs for your reference.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
  • I'm doing the request using the `webview`, so basically the request is made by the actual server. The issue is related to `electron` as the code should be at least sending the headers correctly. – Jaeger Jul 30 '20 at 22:33
  • Yes, but still you'll need to configure the CORS in the server, no headers required from the client-side. I ran into a problem a few months ago, before then it was working. May i know which server you are using? – Kiran Maniya Jul 30 '20 at 22:46
  • I'm basically sending requests to a visited webpage, therefore, I have no idea about the actual server. I'm using the `ipc` to send a request from the same page and act like a button was clicked. – Jaeger Jul 31 '20 at 00:12
  • @Jaeger You have only one solution I already mentioned, You must have CORS configured in server. When you'll set that up, The server will send `Access-Control-Allow-Origin` header before response and you'll be able to fetch response. Please refer to the MDN docs for more clarification. – Kiran Maniya Jul 31 '20 at 00:22
  • Apache server uses .htaccess file and ngix uses ngix.conf file in server, you need to configure it as given in https://stackoverflow.com/questions/10093053/access-control-request-headers-is-added-to-header-in-ajax-request-with-jquery/55584963#55584963 – Kiran Maniya Jul 31 '20 at 00:25