0

I'm making a Chrome extension that makes a POST call to the website in the current tab. This is to a website behind a login (specifically Shopify). The call works fine when made in the Chrome console, but when made from the background.js of extension I get a 303 redirect and then a 400. The call broadly follows this model:

$.ajax({
  type: "POST",
  url: 'https://.../apply_changes.json',
  contentType: 'application/json',
  headers: {          
    'Accept': "application/json, text/javascript, */*; q=0.01",   
  }
  data: JSON.stringify({"key":"value",...}),
  success: function(data){
   alert(data);
  },
  failure: function(errMsg) {
    alert(errMsg);
  }
});

Is there a way to make this call work in the extension as it does in console? Any help is appreciated!

user2850751
  • 53
  • 1
  • 1
  • 9
  • 1
    400 error is bad formatting or bad routing. Perhaps that is the trouble with the extension call, bad routing. Would help if you explained more of what you're trying to do. It is not clear. Front-end shop? Back-end shop? What is endpoint? – David Lazar Nov 24 '19 at 21:52
  • Hi, thanks for responding. What might be bad routing? It's a simple POST request to submit information to the site, and the same request works from the Chrome console. – user2850751 Nov 25 '19 at 00:28
  • You sure that's what your code looks like? You have a syntax error around the `=` for `contentType` – Phil Nov 25 '19 at 04:35
  • 1
    You probably need to use `withCredentials` to share the same session as the page, ie `xhrFields: { withCredentials: true }` – Phil Nov 25 '19 at 04:48
  • Edited to fix contentType, and to fill the Accept header. I typed these quickly from memory. – user2850751 Nov 25 '19 at 15:47
  • I tried with `withCredentials`, but I'm still getting the 303. – user2850751 Nov 25 '19 at 15:47

1 Answers1

0

I ended up finding a workaround: injecting the script directly on the page (Insert code into the page context using a content script).

user2850751
  • 53
  • 1
  • 1
  • 9