Unsuccessful attempt to send data using AJAX on the API

I'm at a loss here - I've tried everything, and I just can't seem to figure out what the issue is. I'm attempting to make a 'POST' request to my teacher's server for a homework assignment, but for some reason it's not working. I've been stuck on this problem for over 2 hours now.

Below is the code I've written:

const xhr = new XMLHttpRequest();
const url = 'https://3uc5taw99i.execute-api.us-east-1.amazonaws.com/latest/attendees';
let attendee = JSON.stringify({firstName: 'Nikola', lastName: 'Nikola', email: 'Nikola', dateBirth: '13.12.1869'});
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
    if (xhr.readyState === XMLHttpRequest.DONE) {
        console.log(xhr.response);
    }
}
xhr.open('POST', url);
xhr.send(attendee);

Answer №1

If you are encountering The 401 Unauthorized error, make sure to include the authorization token variable in your request.

xhr.setRequestHeader('Authorization', '~Token~');

Another API will provide the authorization token for you.

const xhr = new XMLHttpRequest();
const url = 'https://3uc5taw99i.execute-api.us-east-1.amazonaws.com/latest/attendees';
let attendee = JSON.stringify({firstName: 'Nikola',lastName:'Nikola',email:'Nikola',dateBirth:'13.12.1869'});
xhr.responseType = 'json';
xhr.onreadystatechange = function(){
  if(xhr.readyState === XMLHttpRequest.DONE){
    console.log(xhr.response);
  }
  else{
    console.log(xhr.status); 
  }
}
xhr.open('POST',url);
xhr.send(attendee);

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

Add the information to the current JSON file

I'm currently working with an existing JSON file and attempting to add a string into it. However, whenever I write to the JSON file, the new line characters disappear and the formatting gets altered. Check out the code snippet below: #!/usr/bin/pyth ...

Using Javascript to make a call to a RESTful endpoint

My current challenge involves attempting to make a call to the Spotify API from JavaScript: function callAPI() { var xhttp = new XMLHttpRequest(); xhttp.open('GET', 'https://api.spotify.com/v1/search?q=Muse&type=track'); ...

Unable to decrease the width of a div element in Vuetify and Nuxt

As I work on creating a dynamic form with fields that need to occupy only 50% of the size of a regular field, I encounter different components based on data provided by Vuex. The use of v-for in Vue.js helps me loop through form objects and render the app ...

Scroll with Angular to Move Elements

Is there a way to convert this Jquery Code into an angularJs directive? http://jsfiddle.net/MMZ2h/4/ var lastScrollTop = 0; $("div").scroll(function (event) { var st = $(this).scrollTop(); if (st > lastScrollTop) { $('img').a ...

In JavaScript, the event.defaultPrevented property will never be set to true

I've been experimenting with events in my script, but I'm struggling to understand the functionality of event.preventDefault()/event.defaultPrevented: var e = new Event('test'); e.defaultPrevented; > false e.preventDefault(); e.d ...

There seems to be an issue with the Redux connect() higher order component not functioning properly when passing an action creator and

Encountering an issue with the Action creator in react-redux when using the mapDispatchToProps function to return an object to pass into the connect HOC. The strange thing is that it works fine when passing the Action creator directly into the connect HOC, ...

What is the method for switching on and off responses for initial level comments?

Is there a way to manage long comments and replies on my website's nested comment system? I attempted to Hide/Toggle replies upon page load using Ajax, but it was unsuccessful. I added an id to the child comments container and used code to achieve th ...

Using PHP to send asynchronous requests to the server can greatly enhance

I have almost completed my project, but I am facing an issue with reading the data sent to the server. function main() { jQ(document).on("keyup", "form input", function () { var data = new FormData(); var value = jQ(this).val(); da ...

Endless repetition in Angular with ng-bind

After multiple attempts, I have not been successful in getting my get_tracking() function to properly return an object with the summary property. In order to resolve this issue, I am seeking a solution to either bind the summary property to the pack object ...

Can you explain the process of sending an AJAX request and managing it on a web server coded in C?

Could someone please provide an example of an AJAX request that retrieves a global variable stored on a webserver written in C? I am unfamiliar with JQuery and AJAX, so I would appreciate any guidance on how to accomplish this task. Thank you in advance ...

How to suppress certain attributes (such as @Value and @id) in a Java application when working with Fhir JSON data

I have a REST API that is required to generate Fhir xml as well as Fhir json. Java classes were created using xsd and the XML output is functioning perfectly. However, I am facing an issue with the json object where unwanted attributes are being added whic ...

Loop through an array of objects in order to identify the key that holds the highest sum of values

const repos = [ { JavaScript: 82061, CSS: 4992, HTML: 1992 }, { Ruby: 47816, HTML: 8638, JavaScript: 4419, CSS: 1842 }, { CSS: 5006, JavaScript: 812, HTML: 336 }, { Ruby: 1898 }, ]; The API has responded. Each element in the array represents a us ...

Creating an interactive animation of bouncing balls within an HTML5 canvas using JavaScript

function refBalls(){ var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var circles = [{x:40,y:100,r:20,color:'black',vx:5,vy:10}] function draw(){ ctx.beginPath(); ctx.arc(circles[0].x, circles[0].y, circles[0].r, ...

Leveraging JSON to access arrays in React

In my current structure, I have: const [res, setRes] = useState({ question: "", option: [, , , ,], answer: 0, }); I am looking to update the values in the option array based on their indices. For example, setting option[3] to 2 ...

Firefox does not support event.dataTransfer.files for drag and drop operations

Currently, I am implementing drag and drop functionality into one of my projects and encountering an issue specifically with Firefox. The code I am using for testing purposes is as follows: document.getElementById("folder_files").addEventListener("drop", ...

Capture the responseBody data within a Newman script from a Postman collection and save it to a

I'm trying to execute a script.js using newman with a locally saved postman collection. The call is successful in postman, returning a token in the response body that I need to access. I would prefer not to open postman every time just to get the res ...

What steps do I need to take in order to display my data in a dynatree

Currently, I am in the process of developing a web application and incorporating dynatree for structuring purposes. EXAMPLE: Node 1 + Node 1.1 + Node 1.1.1 + Node 1.1.2 + Node 1.1.3 My goal is to introduce a child node (+Node 1.1.3.1) within th ...

transforming a string into a pandas dataframe using Python

I have a String variable with data that I would like to convert into a data frame using Python. I need someone to provide guidance on how to proceed. Data : data1 Name Space -------------------- ...

What if the targeted URL is unresponsive and not returning any results?

Here is an example of the code I'm using to send a request to a URL at a fixed time interval. $(document).ready(function() { var counter = 1; $.doTimeout( 1000, function() { $.ajax({ type: "GET", url: "<%=e ...

What is the method for adjusting the width of a v-card based on its height?

I have a changing number of v-cards that are displayed based on their quantity. My goal is to maintain an aspect ratio of 16/9, but only adjust the width. To achieve this, I believe I need to make the width dependent on the height, although I am unsure ho ...