Encountering an issue when receiving a 400 status code in axios

Currently, I am utilizing Vue along with Axios to construct a single page application frontend. In situations where the server responds with a 400 error in CROS, the browser's console displays the following:

(2) POST http://dev.sportx.one/api/token/ 400 (Bad Request) **This line logged twice**
Failed to load http://dev.sportx.one/api/token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.
Error: Network Error
    at e.exports (spread.js:25)
    at XMLHttpRequest.l.onerror (spread.js:25)

The request is being sent like this:

api.request({
 url: '/api/token/',
  method: 'post',
  data: {
    email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a49d9396979396919693e4d5d58ac7cbc9">[email protected]</a>',
    password: 'aaaaaaaA'
  }
}).catch(err=>{console.log(err)})

Upon checking the browser's network inspector, it shows that 2 requests (OPTIONS and POST) have been successfully executed as intended.

If the server responds with a 20X status without any errors caught, everything runs smoothly. However, when testing the same code on a new HTML file with only Axios script loaded, the same errors persist.

Answer №1

Make sure to include 'Access-Control-Allow-Origin': '*' in your code.

Answer №2

It is important that preflight requests do not result in a 400 error.

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

Tips for running Typescript files on a server in the background

I am currently working on a NodeJS application that consists solely of TypeScript files, without any JavaScript files. I'd like to run it on my server in the background. Any suggestions on how I can achieve this? I experimented with using the npm pa ...

The useEffect function is repeatedly making API calls within a component, despite not having any dependencies specified

As a newcomer to React.Js, I'm encountering an issue with useEffect repeatedly calling an API without any specified Dependency. Is there another approach I should consider? The relevant file is located at: /pages/dashboard/speaking/[slug].js } else i ...

Efficiently Incorporating JavaScript Variables into DataTable's aoData

I am facing an issue with a variable named search_gen, which is generated through an ajax request (shown below). var search_gen; $.ajax({ type: "POST", url: link+module_name+'search_generator/'+module_active, dataType: "text", as ...

I'm having trouble with the routing of a Node.js REST API built with Express and Mongoose

I am currently in the process of constructing a RESTful webservice by following a tutorial that can be found at: However, I have encountered an issue where it is returning a CANNOT GET/ reports error. Despite my efforts to troubleshoot and rectify the pro ...

There has been a JavaScript error detected in the browser process of GithubElectron

After utilizing npm to install "electron-packager", I proceeded to run the following command: electron-packager . Johnston --platform=darwin --arch=x64 --version=0.36.0 Upon attempting to launch the "Johnston.app", I was met with an error message (withou ...

Differences between HTML Markup in PHP and Javascript

I have been working on various tasks involving AJAX requests to fetch data from the backend of my website when a thought crossed my mind. Is it more efficient to style the HTML in PHP before sending it to the client, or should I send the data as JSON and t ...

Transferring data between functional components in ReactJS and dealing with the output of [object Object] or undefined

I'm struggling with passing a prop in functional components that are both located in the same .js file. I previously attempted to seek help for this issue, but unfortunately, it wasn't productive. My goal is to extract the member_id from the GET ...

The dilemma of selecting objects in Three.js

Currently, I am developing a small web application that utilizes three.js. However, I have encountered an issue: I created a prototype with my three.js content and everything functions correctly (the canvas size in the prototype matches the browser window ...

regex for extracting a hyperlink tag from an HTML snippet in a server-side environment

On the server-side, I have an HTML page source stored as a string. My goal is to extract <link rel="icons"... elements from the string and store them in an array. There may be multiple <link rel="icons"... elements with the same starting tag, and I ...

The Jquery Ajax .load() function is not working properly

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Tabs - Expanding content</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> < ...

Ways to include ".com" content statically in HTML on a website with a .org domain

I need to modify the content of a webpage in HTML. Example 1: Whenever I change the domain name to end with .com, the URL within my content automatically updates to www.example.com. Here is an example: dotCOM Example 2: Similarly, if the domain name en ...

What is the best method to obtain the following id for an image?

Whenever I hover over the li with the number 3, I am getting the wrong number for the next id in the img tag. The first time, I get next id = 4 which is incorrect. But on the second hover, I get next id = 0 which is what I actually need. How can I correctl ...

custom popup click event in angular leaflet directive does not work as expected on iOS devices

For the development of a map on iOS devices, I am utilizing the angular leaflet directive and ionic. I am facing an issue where the popup is not clickable on the iOS simulator, even though it works fine on web browsers. var html11 = '< ...

Creating a mongoose query to utilize the $all operator within an array

Consider this dataset: [{ "users": [{ "name": "one" }, { "name": "two" }] }, { "users": [{ "name": "one" }, { "name": "three" }] }, { " ...

deleting a row from a table once XmlHttpRequest is finished

My situation involves an HTML table containing multiple rows, each of which includes an id attribute and a delete button within a cell. To handle the deletion process, I have implemented an ajax call attached to the delete button using the following code: ...

The integration of angular 1.3.15 with html5mode for deeplinking, routing, and hash functionality

I have implemented html5mode true and included <base href="/"> within the head tag. Everything appears to be functioning properly at first, but upon page refresh, an error is encountered. The requested URL /updated_1/work was not found on t ...

Unable to execute PHP alongside a JavaScript event listener

Using PHP, I am creating a canvas for writing and the text output will appear in a textarea (handled by other functions). There are additional input tags like a title to gather user input. The values from these input tags (title and textarea) will be submi ...

Change the background color according to the user's input text

I want to create a text box where users can input color keywords like blue, lime, or black. When they click submit, I want the background color of the page to change accordingly. This is what I have so far: <label for="color">Color: </label> ...

How to identify generic return type in TypeScript

My goal is to develop a core dialog class that can automatically resolve dialog types and return values based on the input provided. I have made progress in implementing this functionality, but I am facing challenges with handling the return values. Each ...

What is causing elements like divs, paragraphs, or text not to display within an ion-item after an ion-input is added?

I am currently working on validating a simple form and everything seems to be functioning properly. However, I have encountered an issue with displaying text messages within an ionic 3 list item. The ion-item consists of an ion-input element. When I place ...