0

my stuff works fine with Fiddler and I get desired result. But when i do it on web page thru JQuery AJAx, it says "Not Found". I am struggling since sometime but couldn't get around.

My Controller method is like this

[Route("AddDonors/")]
[HttpPost]

public dynamic addDonors(localDonor localDonor)
{

            return localDonor;
}

This is how i am calling from web page

 var userInput = { 'FullName': 'Lakshman', 'Mobile': '9924210053' };


            $.ajax({
                type: "POST",
                url: "/AddDonors",
                data:  userInput,
                error: function (result) {
                    alert(result);
                },
                datatype: "json"
            });

this is the model class

 public class localDonor
    {
        public String FullName { get; set; }
        public String Mobile { get; set; }


    }

API registering and other settings are just fine since this works in fiddler.

Please help. Thanks.

Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48
  • What URL does it show in Fiddler or in browser's request log (e.g. in Chrome's Developer Tools) when you run the AJAX code? – twoflower Feb 29 '16 at 18:55

5 Answers5

2

I strongly suspect that the url in your AJAX request is to blame (404 - Not Found) the request can't be routed to a controller for processing.

Without knowing what your full controller looks like and if you have a RoutePrefixAttribute on this specific controller I can't say what the url should be.

I would suggest you monitor network traffic in your browser developer tools (press F12) and compare the request url for your failing POST request to those of your successful requests in Fiddler

If your webpage is created in ASP.Net MVC as part of the same web project you may want to generate the url server side in future see Construct url in view for Web Api with attribute routing. The @Url helper is only available within your .cshtml files though so you will not be able you shift your JavaScript code to a separate .js file.

Community
  • 1
  • 1
Amith Sewnarain
  • 655
  • 6
  • 11
1

i was able to solve the issue by changing the url to url: "AddDonors",

Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48
0

Try to put [WebMethod] attribute.

[Route("AddDonors/")]
[HttpPost]
[WebMethod]
public dynamic addDonors(localDonor localDonor)
{

            return localDonor;
}

Hope this works!

Mohamed Alikhan
  • 1,315
  • 11
  • 14
0

Try this for your POST data

var userInput = JSON.stringify({ 'FullName': 'Lakshman', 'Mobile': '9924210053' }),
Vivek N
  • 991
  • 12
  • 22
0

I had the same error.

As you are using ASP.NET, try making all AJAX calls using the @Url.Action helper.
I don't know why, but in some situations in ASP.NET passing the URL as a String doesn't work.
And try passing your data like the code below
Your modified code should look something like this:

$.ajax({
                type: "POST",
                url: "@Url.Action("AddDonors", "ControllerName")",
                data:  { localDonor: userInput },
                error: function (result) {
                    alert(result);
                },
                datatype: "json"
            });
henrique romao
  • 560
  • 1
  • 8
  • 32
  • @LakshmanPilaka Try changing the type to `string` in your Controller Action, and then, in yout `AJAX`, change to `datatype: text` and pass the data like this: `data : "localDonor="+JSON.stringify(userInput)` – henrique romao Feb 29 '16 at 18:46
  • @LakshmanPilaka Take a look at this http://stackoverflow.com/questions/17370062/posting-json-data-via-jquery-to-asp-net-mvc-4-controller-action – henrique romao Feb 29 '16 at 18:57