Using Vue with Nuxt and Axios for cross-domain POST requests with a proxy

I've been attempting to perform a cross-domain POST request using nuxt and axios

this.$axios.$post('https://hooks.zapier.com/hooks/catch/111111/xxxxx/', { name: 'Jose', })

Encountering CORS blocking, I decided to try using nuxt/proxy with the following setup:

proxy: {
    '/zapier': {
      target: 'https://hooks.zapier.com',
      pathRewrite: {
        '^/zapier': '/',
      },
    },
  },
this.$axios.$post('https://localhost:3000/zapier/hooks/catch/111111/xxxxx/', { name: 'Jose', })

❌ However, I am still receiving the same error response:

{
  "message": "Network Error",
  "name": "Error",
  "stack": "Error: Network Error\n    at createError (http://localhost:3000/_nuxt/commons.app.js:771:15)\n    at XMLHttpRequest.handleError (http://localhost:3000/_nuxt/commons.app.js:306:14)",
  "config": {
    "url": "https://localhost:3000/zapier/hooks/catch/111111/xxxxx/",
    "method": "post",
    "data": "{\\"name\":\"Jose\"}",
    "headers": {
      "Accept": "application/json, text/plain, */*",
      "Content-Type": "application/json;charset=utf-8"
    },
    "baseURL": "http://localhost:3000/",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1
  }
}

Could someone help me identify what's causing this issue?

(Is it even feasible? My site is hosted on Netlify, so it needs to work on the client-side)

Answer №1

We're so close...

While in development mode, I was able to achieve the desired outcome using this configuration:

  axios: {
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
  },

  proxy: {
    '/zapier/': {
      target: 'https://hooks.zapier.com/',
      pathRewrite: { '^/zapier/': '' },
      changeOrigin: true,
    },
  },

Unfortunately, the proxy setup doesn't function on static websites 😪

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Testing an Angular directive, like ng-if, through unit testing

My custom service and directive work together to remove DOM elements based on specific criteria. The service checks a string parameter, while the directive removes the element it's attached to if the check fails. This functionality is supported in I ...

Error Encountered: unable to perform function on empty array

I've encountered an issue with my Vue JS 2.6.10 application after updating all packages via npm. Strangely, the app works perfectly fine in development environment but fails to function in production. The error message displayed is: Uncaught TypeErr ...

Utilizing Socket.io within secured routes

I'm currently working on a web application that involves user authorization and the ability to chat with others. I've encountered an issue where, despite including e.preventDefault() in my JavaScript code for the submit button, the page still ref ...

Parsing query parameters in Express using the "+" symbol

I am running an Express server and have set up the following configuration in my routes: app.use(express.urlencoded({ extended: false })); On the frontend, I am making a request using Angular: let params = new HttpParams().set('info', info); i ...

The use of fs.writeFileSync is invalid and will not work for this operation

Encountering an issue while working with fs in next.js, receiving the following error message: TypeError: fs.writeFileSync is not a function Here's a snippet from my package.json: resolve: { fallback: { "fs": false }, } ...

I am struggling to comprehend the code for a PHP form that triggers a JS OnUpdate event, which then uses AJAX to retrieve data from MySQLi

I want to give a special thanks to Alon Alexander for providing the JS and AJAX code, even though I don't fully comprehend it. I am more comfortable using PHP/JS without jQuery, but I am struggling to make it function as intended. My current issue in ...

Arrange the items in a list in JavaScript in descending sequence

How to sort a list of records in JavaScript in descending order? var number; //dynamic number retrieved from API var test; //dynamic text retrieved from API for (var i; i <= accList.length; i++) { var odlist = 'you have :' + test + number ...

Clicking on the modal button causes the code to run multiple times in JQuery and JavaScript

Hello, I'm experiencing an issue where the code inside a modal is being executed multiple times when the modal button is clicked. For example, if I click the modal button once, the code runs once; if I click it twice, the code runs twice, and so on. ...

Unraveling and interpreting all incoming requests to my Node.js application

Looking for a simple method to identify and decipher all encoded characters in every URL received by my Node.js application? Is it possible to achieve this using a middleware that can retrieve and decode symbols such as & ? ...

Next.js encountered an issue: React.Children.only function was expecting to receive only one React element child, but received more

I have created a component named Nav located in the components directory. Here is the code for it: import Link from 'next/link'; const Nav = () => { return( <div> <Link href="/"> <a> Home </a> ...

When using Node.js with Nginx, requests are timing out at the Nginx level before the processing is completed in Node.js

I have a setup where I use Nginx and Node.js servers. The process involves uploading a file from a browser to Nginx, which then forwards it to Node.js for processing. However, I've encountered an issue when dealing with large files - the upload crashe ...

The table header row mysteriously vanished after implementing the "stacked" property

A few days back, all of my <b-table> elements that were utilizing the stacked="md" property suddenly had the headers row disappear. Removing the property brings back the headers row, but I really need this property for responsive stacking on smaller ...

"The issue I'm facing with Next.js is that the fetched data is not displaying in the template. Additionally, I'm

I am encountering an issue with my API data where it is being called twice in the terminal. Additionally, I am trying to display this data on my page template but for some reason, it is not showing up. Here is the code snippet in question: Here is my code ...

Attempted to remove Child Div using Jquery, but it did not get removed as expected

Trying to remove child divs inside a parent div? There are several methods, such as: $(".ChildDiv").html(""); $('.ChildDiv > div').remove(); $(".ChildDiv").html(""); $("#ParentDiv").html(""); $(".ChildDiv div").remove(); $(".ChildDiv di ...

When PHP echoes a variable, it may reveal one number, but if that variable is then stored in a cookie and later evaluated, a different number may

I am encountering an issue with a variable within a post array called childnumber. This variable is originally a javascript variable sent to the server via an Ajax post. In the PHP script, I have the following: $newValue = $_POST["childnumber"]+1; After ...

How can we determine if a v-for loop in Vue.js that includes a v-if condition has rendered any elements?

In the scenario where the code below is implemented: <template v-for="(item, index) in items" v-if="item.name === 'foo'"> <p>{{ item.name }}</p> </template> Is there a way to display a message if this loop doesn' ...

Issues with NodeJs Express routes execution

After testing, I found that only the default route "/" is working in my code. Many similar issues involve routers being mounted to paths like "/auth" or "/user". Even when I tested the default router mounted to "/", it still isn't functioning properly ...

Trouble initializing Google Maps in an Angular directive

I'm currently working on integrating the Google Maps API into an Angular website, but I'm encountering initialization issues. Within my HTML page, I'm utilizing bootstrap nav nav-tabs and switching between tabs using an Angular controller. ...

Retrieving JSON data using the fetch method in JavaScript

Currently, I am attempting to retrieve a JSON array using AJAX. Upon calling my test.php file, the following content is returned: [{"name":"James","age":24},{"name":"Peter","age":30}] This is the JavaScript code that I am using: var persons = new Array( ...

Refreshing canvas with new javascript content following an ajax request

Issue: When using ajax driven pagination to load canvas based portfolio items, script tags are not within the canvas tags. Attempted Solution: Tried placing script tags before canvas tags, but scripts still did not load (seems to be a loading script issue ...