'JQuery Post Request Body Not Received with CORS

I'm sending a post request with JQuery.

$.ajax({
      url: 'http://localhost:8082/login',
      method: 'POST',
      data: JSON.stringify(data),
      contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
      success: function(data, status, xhr) {
        console.log(xhr.responseText);
        return(true);
      },
      error: function(data, status, xhr) {
        console.log(xhr.responseText);
        return(false);
      }
    });

The server receives the request but without the payload. This works without CORS, so I'm not sure what the issue is. My CORS setup is very basic and should allow everything.

app.use(cors({ origin: true, credentials: true}));

Network traffic shows the origin is correct and that the payload is being sent.

enter image description here



Solution 1:[1]

I forget to include bodyParser.

Exact solution was to add this to the node backend file that handled the JQuery POST

const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended:true}));

Solution 2:[2]

I basically rubber ducky'd this thing. After typing this up, I figured it out. I looked at the instance methods again and saw that executeJavascript is one of them so I don't need to get to webContents at all...

So this works and I thought I would share:

view.webContents.mainFrame.frames.forEach(frame => {
  const url = new URL(frame.url)
  if (url.host === 'sub.whatever.com') {
    frame.executeJavaScript('myCodeHere')
  }
})

I used the host name in my instance but you could use frame.processId instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 jtvberg