The browser returns a 404 error for the Web API POST request, but it is successful when made using PostMan and Swagger

I encountered a peculiar issue with my Web API: it returns a 404 error when called from AJAX, but functions perfectly fine on PostMan and Swagger.

Here is the post method code snippet:

// POST api/<controller>
[HttpPost]
public int Post(string url, string RData, string aid, string AuthToken)
{
       //do something
}

Below is the jQuery AJAX call implementation:

 function SaveData() {
            var $form = $("#formMain");
            var data = Serialize($form);
            var RData = JSON.stringify(data);
            var Url = window.location.href;
            var aid = "12332";
            //Get Auth Token

            $.ajax({
                url: "/api/Register/MyAPIKey", //Getting Auth Token first
                type: "GET",
                success: function (res) {
                    let AuthToken = res;
                    //After receiving Auth token, finally submit the registration data. This throws 404 error only on browser.
                    $.ajax({
                        type: "POST",
                        url: '/api/Register',
                        data: {
                            'url': Url,
                            'RData': RData,
                            'aid': aid,
                            'AuthToken': AuthToken
                        },
                        success: function (data) {
                            //Show success message
                        }
                    });
                }
            });
        }

The error message displayed on the browser console states:

Message: "No HTTP resource was found that matches the request URI 'http://localhost:54318/api/Register'."
MessageDetail: "No action was found on the controller 'Register' that matches the request."

There is also a screenshot from Postman attached for reference showing an expected -1 response even though there may be some validation errors present in the API method. The important thing to note is that the status is 200.

https://i.sstatic.net/UxiiX.jpg

Answer №1

The problem was fixed by replacing the individual parameters in the Post method with an Object containing all of them as properties.

[HttpPost]
public int Post(PostData Reg)
{
   //do something
}

public class PostData
{
    public string url { get; set; } 
    public string RData { get; set; }
    public string aid { get; set; }
    public string AuthToken { get; set; }
}

Lastly, I configured the dataType in the ajax method as JSON.

dataType: "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

Several middlewares using router.params()

Is it possible to include multiple middlewares as parameters in the function router.params() in Node-Express? I currently have the following setup: const checkAuth = (req, res, next) => {console.log("checking auth"); next()} const checkAuth = ...

I am having trouble getting the jsTree ajax demo to load

I am having trouble running the basic demo "ajax demo" below, as the file does not load and the loading icon continues to churn. // ajax demo $('#ajax').jstree({ 'core' : { 'data' : { ...

Using express.js to transfer an uploaded image to Amazon S3

Currently, I am faced with an issue of passing an image uploaded from a react application through express to a managed s3 bucket. The s3 bucket is created and managed by the platform/host I am using, which also generates upload and access urls for me. So f ...

Experiencing difficulty in successfully updating a React child component when the prop passed from the parent component is

In the backend implementation, I have successfully set up a socket.io, Node, Express configuration that has been tested to work correctly. It is emitting the right number of broadcasts to the appropriate client using socket.emit("team_set", gameI ...

The flexslider isn't updating slides when clicking on thumbnails

Recently, I incorporated a CSS code to display an overlay when hovering over the thumbnails in my flexslider slideshow. However, after adding this code, I noticed that clicking on the thumbnail does not produce any action. .flex-control-nav li{ positi ...

Guide to utilizing the Fetch API to send text/plain content

Attempting to utilize the Fetch() API for posting text/plain data: fetch('/test', { method: 'post', headers: { 'Content-Type': 'text/plain' }, body: "this is a test" }) .then(respon ...

Is it possible to set an ASP.NET Core controller as internal?

One essential requirement for ASP.NET (and Core) controllers is for them to be marked as public. However, I am encountering an issue with a controller that relies on something marked as internal in its constructor. Furthermore, this dependency is intercon ...

The URL specified for the Django project could not be located when running on the httpd server

Hey there, this post may be a little lengthy but I appreciate your patience. Currently, I'm working on a Django project running on an apache2 server. In my index.html file, I have two images embedded in anchor tags: <a id="pop1" href="#"> <i ...

Session storage conditional statement is not behaving as anticipated in JavaScript

I am currently working on session storage functionality where a user must select one radio button on the first page and then click the next button. On the second page, a div is supposed to show up. On the first page, I have created a function with a set of ...

Is it possible to create a translucent glass floating box over HTML elements, similar to the frosted glass effect seen in iOS7?

Creating an overlapping div with a frosted glass effect typically requires using an image background (like ). I am interested in having a floating div (position:fixed) that can apply a frosted glass effect over any content below it, whether it be an image ...

Invoke jQuery within a nested context once more in the jQuery method sequence

Is it possible to do something like this? jQuery(selector).jQuery(subselector).command(....) Here is the current code snippet: $(' .species') .filter(function () { return !$(this).data('hidden_code_ready'); } .wrapInner('<spa ...

Ways to extract certain characters from each element in an array

I'm attempting to make the first four characters of each element in an array (specifically a list) bold, but I am only able to select entire strings: $("li").slice(0).css("font-weight", "Bold"); Is there a way for me to indicate which characters wit ...

Display the dropdown selected value within a Laravel Blade template

I am facing an issue with passing the selected value from a drop-down to another Blade file in Laravel. I have tried using Ajax and jQuery, but it doesn't seem to work for me. I want to display the selected value on the content.blade.php page without ...

My eCommerce website is currently experiencing some technical difficulties that need to be addressed

I'm in need of assistance with a particular error I encountered. I was following an ecommerce example application and everything seemed to be going smoothly until I clicked on "Shop Now." At that point, I received the following message: Server Error T ...

The Epub text box feature is malfunctioning

I have a quiz task in an epub format where users need to enter their answers in a text box after reading the question. However, I am facing an issue where the text box does not display the keyboard for typing the answer. Is there a solution using javascr ...

The success function in Ajax jQuery isn't being triggered even though the code is running

When using AJAX, I am trying to pass a json variable. Here is the ajax function I have: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script> function emailCheckForFrontend() { var key='7 ...

ASP.Net Core Razor: Troubleshooting Null JSON Value in Ajax Requests

I am currently using .Net Core Razor for my application and encountering an issue with passing values to the controller when calling the action from Ajax. Below is the script I am using: var table = $('#dataTable').DataTable(); var data = table. ...

Stop Users from Inputting Decimal Numbers with JavaScript

Just starting out with Java Script and looking to restrict users from entering decimals in a field using JavaScript. I attempted this approach but encountered the following error. Syntax error on token "_$tag____________", invalid AssignmentOperator < ...

Ways to create a fixed top navigation bar that adjusts content similarly to a non-fixed navbar

I'm looking to achieve a fixed navbar at the top that pushes down content when I open the collapse menu, similar to how it functions in static or regular navbars. Something like this example: (https://getbootstrap.com/examples/navbar-static-top/) but ...

Tips for fading out two elements after completing a drag and drop action

Visit this Codepen for more examples - Codepen I have been developing a drag and drop feature in my application. Once the item is dropped, it transitions from red to green and fades out smoothly. The droppable element behind the draggable should also fad ...