Encountering a 'Not Found' error message when sending an Ajax post request

Having trouble with an AJAX call in my JS file in Durundal.

var dataJSON ={ID : "jo"};
self.js = $.ajax({
    type: "POST",
    dataType: text,
    url: "http://localhost:53081/api/File",
    data: JSON.stringify(dataJSON),

    error: function (xhr, status, error) {
        alert(error);
    },
    success: function (json) {
        alert("Data Returned: " + JSON.stringify(json));
    }
});

Here is my REST API:

[HttpPost]
public string upload(string ID)
{
    string givenId = ID;
    return givenId;
}

However, when I call this method, I am only receiving an error alert. What am I doing wrong?

Update:

I have made updates to my code, but now I am encountering a "Not found" error.

Answer №1

Convert a string to Text:

self.js = $.ajax({
    type: "POST",
    dataType: **Text**,
    url: "url",
    data: JSON.stringify(dataJSON),

    error: function (xhr, status, error) {
        alert(status);
    },
    success: function (json) {
        alert("Received Data: " + JSON.stringify(json));
    }
});

Check out the list of data types and their representations here.

Answer №2

If you're struggling to get the method working, consider renaming it to PostFile. I encountered issues until I followed the correct naming convention, despite having the [HttpPost] attribute at the start of the method.

Additionally, experiment with changing your dataType to "json" and including the content type:

dataType: "json",
contentType: "application/json"

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

Unlock the powers of Express, Passport, and Redis sessions!

Lately, I have been utilizing the default MemoryStore for my Express sessions and everything has been running smoothly. However, I encountered a setback where all session data was lost between restarts. To address this issue, I am now attempting to configu ...

Guide on crafting a scrollable and touch-responsive grid using CSS and JavaScript

I am seeking guidance on creating a grid with a variable number of columns and rows. It should be contained within a separate div and should not interfere with other objects or alter the parent's size. The grid needs to be square and I am currently u ...

Sequelize synchronization does not generate the database table

I am currently working on a project with the following directory structure: ├── index.ts ├── package.json ├── package-lock.json ├── src │ ├── controllers │ ├── models │ │ └── repository.ts │ ├ ...

An issue has emerged: React cannot render objects as children. The culprit appears to be an object containing keys such as {

My sanity schema, named blogs, includes a reference field called author. I am trying to use blog.author in order to fetch the author's name for my blog, but I keep encountering an error. https://i.stack.imgur.com/haotL.png The code in my Sanity blo ...

Looking for a way to dynamically append a child element within another child

Struggling to include a new child within a specific child in Json myObject:any[] = []; this.myObject = { "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, } } addF ...

When a function is transferred from a parent component to a child component's Input() property, losing context is a common issue

I have encountered an issue while passing a function from the parent component to the child component's Input() property. The problem arises when the parent's function is called within the child component, causing the this keyword to refer to th ...

ajax does not update the designated value upon a successful call to onSucess

Although this project is hosted on Shopify, I will provide explanations for everything so you don't need to understand how Shopify works. Here is the website I will be referring to: (). This pertains to the stars you see and their readonly status on ...

The ngModel directive automatically clears the checkbox's Checked status

Currently, my Angular code is set up to validate a checkbox using ng-model: <input type="checkbox" name="news1" value="news1" ng-model="news" <c:if test="${xxxx == yes'}">checked="checked"></c:if>> <label ng-click="news1();"&g ...

Trouble with setInterval not refreshing the HTML element

I'm currently working on a script that should update the value of an element every second. However, I've encountered an issue where the element only updates the first time and then stops. Strangely, there are no errors appearing in the console ei ...

Check if the condition is met, and if it is true, return with Ajax and PHP even

My form submission using AJAX and PHP is working fine, but I'm encountering an issue with the checkbox not behaving as expected within the if condition. Even though var_dump($check1); correctly outputs false or true, there seems to be a persistent is ...

The process of eliminating body padding in Nuxt.js

I'm considering building a website using Nuxt.js because I've heard it's both cool and user-friendly. While I do have some knowledge of vue.js, I'm stuck on a particular issue: How can I remove the padding from the body? I understand ...

The Art of Determining the Text's Baseline

My goal is to create a test that determines whether the text rendered inside an <input> has the same baseline as a label: To achieve this, I want to calculate the baseline of the rendered text in each element and compare their values. Is it possible ...

Sending POST Requests with Next.js Version 14

How can I send a POST request using axios in Next.js 14, I prefer axios but fetch is also okay. I have been getting an AxiosError: Network Error when I try to use axios and TypeError: fetch failed when using fetch. However, it works fine with Postman. I ...

transferring data between react components

I am currently working on breaking down code into smaller components, starting with the snippet below from Rows.jsx. I am struggling to grasp how props are passed between parent and child components. Despite reading several guides and articles, I remain un ...

Discovering the initial element in an array that meets a condition by employing an asynchronous function

I'm facing a rather peculiar issue. I need to locate the first element in an array that meets a certain condition, but the find function must be labelled as async because a promise is required for the search. Just to clarify, the code provided below ...

Is there a way to use a specific keyboard input to alter the characteristics of shapes on my webpage?

Is there a way to change certain attributes of a shape onscreen when a specific key is pressed by the user? For example, can I make it so that pressing "a" changes the color of the shape? I attempted to modify a mouse rollover event to work with the desir ...

Modifying the user interface (UI) through the storage of data in a class variable has proven to be

If I need to update my UI, I can directly pass the data like this: Using HTML Template <li *ngFor="let post of posts; let i = index;"> {{i+1}}) {{post.name}} <button (click)="editCategory(post)" class="btn btn-danger btn-sm">Edit</butto ...

Using scale transformations to animate SVG group elements

I am currently experimenting with an SVG example where I hover over specific elements to expand or scale them. However, I seem to have made a mistake somewhere or missed something important. Can someone offer me assistance? View the demo on JSFiddle here ...

How can you avoid Ajax calls from failing due to frequent requests?

Currently, I am utilizing ajax to retrieve some data. However, I have encountered an issue where the ajax request is triggered by hovering over a button, which could potentially result in multiple requests being sent and overwhelming the server, leading to ...

Transitioning from the login screen to the main page in Ionic with the help of Angular

My code is running smoothly, but I encountered an issue when clicking the login button. It changes the state as depicted in the image, altering the URL but failing to open the subsequent page. Can anyone provide guidance on how to redirect to the next page ...