Receiving a XHR 404 error when trying to upload an mp3 file, even though the directory clearly

I am currently working on a project where users will be able to upload an mp3 file of their choice through JavaScript and AJAX. Here's the code snippet I have so far:

// Creating the file upload button
var uploadBtn = document.createElement("input");
uploadBtn.innerHTML = "Browse...";
uploadBtn.id = "fileToUpload";
uploadBtn.type = "file";

// Creating a submit button
var submitButton = document.createElement("button");
submitButton.innerHTML = "Submit";
submitButton.id = "submitButton";

Once the user selects their file and clicks the submit button (where mp3File and formdata are global variables set elsewhere), this block of code is executed:

mp3File = document.getElementById("fileToUpload").files[0];
formdata = new FormData();
formdata.append("mp3File", mp3File);
var req = new XMLHttpRequest();
req.open("POST", 'mp3_uploads/', true);
req.send(formdata);

However, I keep encountering the following error:

XHR POST [path to my website here]/mp3_uploads/ ---> HTTP/1.1 - 404 - Not Found

Despite having the directory set up on my server, I still receive this error message. While I do have a working solution using a separate HTML form that references a PHP file for mp3 file uploads, I would prefer sticking with the JavaScript / AJAX method if possible.

Any assistance on resolving this issue would be greatly appreciated.

Answer №1

Even though the directory is present, it doesn't consist of a script. Moreover, a directory is incapable of processing a POST request, resulting in a 404 error.

To resolve this issue, consider developing a PHP script (or any other server-side language) that can manage the file and store it in the specified directory.

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

Saving and presenting information within a Cordova application

In my Cordova (Phonegap) app, I am currently working on a feature that involves storing users' personal data such as usernames and scores every 60 seconds. After this interval, I want to display all the users' data, essentially creating a scorebo ...

How can Node.js developers properly utilize PM2 for their projects?

I'm currently contemplating a switch from forever to PM2 as a way to ensure my node application stays up and running. I find myself puzzled by the various recommended methods for initiating a process: $ pm2 start app.js -i 4 # Daemonize pm2 and Star ...

Getting Data from Selected Radio Buttons in AngularJS Using ng-repeat

I am looking to retrieve the data (id, name) of a selected row using a radio button. Here is what I have so far: <tr ng-repeat="list in listTypes"> <td>{{list.TaskId}}</td> <td>{{list.Comments}}</td> <td>{{l ...

Can a specific input value be applied or utilized in any way?

I have limited coding experience, so please forgive me if this is a simple question. I am curious if it is possible to create a feature on a website where user input is utilized elsewhere. Specifically, I am looking to have an input box where a user enter ...

Creating a user-friendly three step wizard on a single webpage using the dynamic duo of AJAX and PHP

I am facing a challenge while trying to set up a 3-step wizard on a single page using ajax. I have created two PHP files for Form2 and Form3 generation. While the first step of creating Form2 worked smoothly, I encountered an issue with the second step inv ...

Tips for composing a single aggregation within a query

In my query, I wanted to find the first record with a CREATE_DATE greater than or equal to a specific date and less than another date. After calculating the duration between these dates, I needed to write another query. However, I was unsure how to combine ...

What is the best way for me to send a confirmation response to my API once a user has clicked on the activation link

I have been attempting to send a confirmation message to my API after clicking on the activation link, but despite numerous attempts, I have not been successful. Please see the activation link below: <script type="text/javascript> ...

Connecting an HTML box to a JavaScript function for the desired outcome: How to do it?

My goal is to connect the input box with id="b" (located inside the div with id="but1") with a JavaScript function that calculates values within an array. Although my junior logic review didn't detect any issues in the code, what mistakes could I have ...

jQuery SlideUp and SlideDown causing Margin Bug in Internet Explorer 7

When clicking the "more info" or "less info" buttons to slide content up or down, a spacing glitch is created in IE7. Using show/hide instead of slideUp/slideDown seems to solve the issue. Is there a way to make sliding work in IE7 without causing this pro ...

The response header does not contain a valid JWT token

Here's a function I've implemented to authenticate users by creating a response header: function verifyUser(res, id) { const payload = { id } const token = jwt.sign(payload, config.JWT_KEY, { expiresIn: '24h' ...

Node.js is encountering an error with the post method where req.body is returning an empty

When utilizing cURL or Postman, the operations perform as anticipated curl -d 'username=Johny Sample&title=My First Post&description=We need some assistance cleaning up after the hurricane&postID=Johny Sample_1' http://localhost: ...

Regular expression to validate the proper file naming convention: 1201_17-11-2015.zip

I am looking to verify if a specific file name follows the correct format. Here is the required format: first four numbers_two numbers-two numbers-4 numbers.zip To achieve this, I will need a regular expression. An example of a file name in JavaScript ...

Enhance your React application by using a personalized hook that allows you to trigger a function

After creating a custom hook to handle uploads to an AWS S3 bucket, I encountered a small issue. Rather than having the hook execute the logic directly, I decided to create an executable function to return instead. However, I am facing a problem where the ...

Using a loop to execute Javascript Promise.all()

I am currently facing an issue where I need to make a web API call twice inside a loop, and then wait for the results before pushing them into a larger array as subarrays. The code snippet below illustrates my approach: var latlngPairs = []; function extra ...

"Prevent further button clicks by disabling it after the initial click with ActionRowBuilder in Discord.Js

Encountering a puzzling issue where I am unable to disable a button after it has been clicked. The option to disable the button does not seem to appear. When attempting to deactivate the button, I utilize the following function: const row = new ActionRowBu ...

jQuery is not defined when importing reactjs, bootstrap, and npm modules

Having trouble using Bootstrap in my React app, as I keep getting an error message saying Error: Bootstrap's JavaScript requires jQuery. I've already imported jQuery and tried various solutions found in other posts like: import $ from 'jque ...

Check the data from the radio button selection

I am facing an issue with reading the values of radio buttons in a list, all of which have the same id. Despite trying to retrieve the value of each individual radio button, my code only reads the value of the first radio button. <script src="//ajax. ...

Switching up the Label Colors in Chart.JS

It's been a while since my last question, so please bear with me if I'm not following the rules. I've attached my current code and a reference image of the chart. I am completely new to working with ChartJS. The situation is a bit unique: t ...

Incorporating list items in a React component is not functioning as expected

When I console.log(this.props), here are my props: list:Array(1): {user: "Jack Nicholson", userid: "5b684ed8d3eb1972b6e04d32", socket: "1c0-Jb-kxe6kzPbPAAAD"} However, despite mapping through my list and using the component <UserItem user={user.user} ...

Is it possible to simultaneously run multiple functions with event listeners on a canvas?

I'm attempting to create a canvas function that displays the real-time mouse cursor location within the canvas and, upon clicking, should draw a circle. I came across this code snippet that reveals the x and y coordinates of the mouse: document.addEve ...