"Troubleshooting 404 error when using Vue.js and vue.config

I'm currently following the Vue JS 2 Tutorial #32 - HTTP Requests using vue-resource to connect to jsonplaceholder.typicode.com. If I don't use a proxy, it triggers a CORS error.

Here's my vue.config.js setup:

module.exports = {
  devServer: {
    proxy: {
      '^/api': {
        target: 'https://jsonplaceholder.typicode.com',
        ws: true,
        changeOrigin: true,
        pathRewrite: { '^/api': '' }
      }
    }
  }
}

When making an HTTP post request:

this.$http.post('/api/posts', {
    userId: 1,
    title: this.blog.title,
    body: this.blog.content,
}).then(function (data) {
  console.log(data)
});

The error that I encounter is:

XHR POST http://localhost:8080/api/posts [HTTP/1.1 404 Not Found 3ms]

I've attempted various solutions such as:

After trying different edits including:

  • Changing '/api/post' to '/api/posts', but still facing issues.

  • Switching from '/api/posts' to

    'https://jsonplaceholder.typicode.com/posts'
    which resulted in another CORS error.

  • Adding pathRewrite: { '^/api': '' } into the vue.config.json proxy configuration, yet the problem persists.

  • Exploring solutions like Proxy changeOrigin setting doesn't seem to work, with no success.

Answer №1

It seems like your request is currently being directed to localhost instead of the intended server. Have you considered modifying this.$http.post('/api/post', { to

this.$http.post('https://example.com/api/post', {

Based on my understanding of vuecli's documentation, proxying the server does not eliminate the need to specify the server's URL in your requests.

Answer №2

Consider restarting your development server. A complete restart is necessary in order to implement the modifications effectively.

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

Steps for incorporating an npm module

Recently, I created an npm package named "sum" with just a main.js file: export function sum (a , b){ return a+b } Here's how my package.json looks like: { "name":"sum", "version":"1.0.0", "description ...

Finding the midpoint of SVG paths and VML shapes

I'm currently experimenting with: My main focus is on obtaining the center points of different countries (to accurately place markers on each country). I've encountered some challenges in achieving this and have attempted various methods, such ...

The values of the elements in the image array are null when not inside the

I need help converting an image into a Uint8Array for use in F5 steganography with the f5stego package. After trying to implement this, I encountered an issue where the imageArray contains all zeroes when printed outside the function, but inside it holds ...

What is the method to deactivate multiple links using jQuery?

Below is the HTML code: <a title="Login" data-href="/MyAccount/Access/Login" data-title="Admin" data-entity="n/a" id="loginLink" class="nav-button dialogLink"><b>Login</b></a> <a title="Register" data-href="/MyAccou ...

Tips on obtaining the element's ID as a function parameter

I am currently learning front-end development and I am just starting to delve into JavaScript. Recently, when I tried to execute a piece of JavaScript code from the backend by passing some element ids, I encountered an error that says Cannot read property ...

Issues with event listeners in Vue 3 render function are causing it to not work

My current method involves using the render: () => functionality to generate html nodes, with everything functioning as expected. However, I am facing an issue where setting an event listener on a button does not trigger the specified function. The onC ...

Optimal approach for incorporating controller As with UI Router

Currently working on a small search application using AngularJS and Elasticsearch. I am in the process of transitioning the app from using $scope to controller As syntax. I have implemented UI Router for managing routes/states. I have been attempting to us ...

Struggling to loop through a child in Firebase real-time database?

I'm struggling to retrieve a nested child from my database with the following structure https://i.stack.imgur.com/ZDs38.png I am attempting to get the URI from the Gallery object. When I log in, I can see this in the console https://i.stack.imgur.c ...

Using JavaScript, you can employ the .split() and .replace() methods on a string value to accurately extract the specific

I'm currently attempting to extract hashtags from a text string by splitting it and removing unwanted HTML tags. Despite my efforts, I haven't been able to achieve the desired outcome. I am seeking guidance on where I might be going wrong. Here ...

Can you explain the distinction between res.render() and ejs.render() when used in a Node.js and Express application?

Incorporating the EJS template engine into my Node.js and Express application has been seamless so far, with no issues encountered. I have been utilizing its functionality and rendering capabilities consistently. However, I have noticed that I typically u ...

Having trouble with the active class in vue-router when the route path includes "employees/add"?

I'm currently developing a project using Vue and implementing vue-router for navigation. A peculiar behavior occurs when the route changes to /employees - only the Employees menu item is activated. However, when the route switches to /employees/add, b ...

What are the best ways to get HTML tags functioning properly within Angular.js?

When trying to print a response from a different server that contains a lot of HTML code, how can I display it in a single container? In jQuery, you can achieve this with the following code: $("#xyz").html("<h1>Hello World</h1>& ...

Angular's parent div height can be adjusted using CSS transitions

I have implemented a CSS transition to create a sliding effect in my pagination. However, I am facing an issue where the height of the containing div changes even when I set the position of the transitioned elements. Here is a snippet of my CSS: .slide { ...

Angular 2 ngSubmit triggers unexpectedly on occasions when it is not supposed to

Currently, I am working on developing an Ionic 3 application with Angular 2 and TypeScript. In the app, there is a form that is responsible for sending data to our server. The issue I am facing is that whenever I click on the following button: <butto ...

How can I efficiently load AJAX JSON data into HTML elements using jQuery with minimal code?

I have successfully implemented a script that loads an AJAX file using $.getJSON and inserts the data into 2 html tags. Now, I want to expand the JSON file and update 30 different tags with various data. Each tag Id corresponds to the key in the JSON strin ...

The issue I am facing is that when I click on a checkbox, only one of them seems to respond

When I click the button, only the first checkbox event is being checked while the rest of them are not. Can someone please provide some guidance on how to fix this issue? $("#cascadeChange").click(function() { //alert("Clicked"); ...

What could be causing Vue to not update data after the next tick?

I am currently exploring the Vue.js nextTick method and I'm puzzled as to why the message isn't updating after nextTick in this particular scenario. new Vue({ el: '#app', data: { message: 'one' }, created() { ...

Guide to extracting a specific segment from req.body in Node.js

I've been attempting to access a specific nested property of req.body, but the output always turns out to be undefined. Here is the code snippet: let dataReceived = JSON.stringify(req.body); console.log(dataReceived); let refCode = ...

Steps to automatically logout the user when the cookie expires in Nuxt

I have successfully implemented login functionality using the official NuxtJS Auth Routes example in my project with express-sessions. This is the content of my app.js file: const express = require('express') const session = require('expre ...

In what way does Codesandbox create a resizable grid layout for components?

I am looking for guidance on implementing resizable windows like those in CodeSandbox and VS Code using React. How does CodeSandbox achieve this functionality? Could someone help me get started in the right direction? ...