On my page is a button which when clicked by the user triggers and Ajax call to my controller which returns an array of dates. This array of dates is then used to populate a datepicker. The problem I have however is when I click the button and trigger the process I get the following error in Firebug:
From which namespace is the HttpPost attribute. I know there is one in System.Web.Mvc and there is one in System.Web.Http. For API controllers you need the last one.
{"Message":"The request is invalid.","MessageDetail":"The parameters
dictionary contains a null entry for parameter 'packageCode' of
non-nullable type 'System.Int32' for method
'System.Collections.Generic.IEnumerable`1[System.String]
GetProductAvailability(Int32)' in
'IcelandMountainGuides_Prototyping.Controllers.ProductsController'.
An optional parameter must be a reference type, a nullable type, or be
declared as an optional parameter."}
I've solved the issue.
If you notice I am passing JSON in my Javascript request and unlike a surface controller, WebAPI seems incapable of breaking it down into its individual values so I had to define an model of sorts and pass that instead:
public class ProductsController : UmbracoApiController
{
[System.Web.Http.HttpPost]
public string[] GetProductAvailability([FromBody] Tour tour)
{
return new[] { "9-5-2015", "14-5-2015", "15-5-2015" };
}
}
public class Tour
{
public int packageCode { get; set; }
}
WebApi - 405 not allowed when called via Ajax
Hi all,
I have been following this guide on implementing a WebApi controller:
http://our.umbraco.org/documentation/Reference/WebApi/
On my page is a button which when clicked by the user triggers and Ajax call to my controller which returns an array of dates. This array of dates is then used to populate a datepicker. The problem I have however is when I click the button and trigger the process I get the following error in Firebug:
When looking at the response method I get is
The following is my controller called ProductsController
The code for the button is as follows:
And the Javascript call I make when the button is clicked is as follows:
My question is why am I receiving this error and how do I circumvent it?
Any help would be greatly appreciated.
This first thing i see is in your ajax you pass data to your api.
Your API method however doesn't have any parameters. I think this is the problem.
Dave
Sorry that was a typo above. I am actually doing the following:
All code looks okay to me.
From which namespace is the HttpPost attribute. I know there is one in System.Web.Mvc and there is one in System.Web.Http. For API controllers you need the last one.
Dave
Well that has made some difference. It must have been using the former as now after explicitly declaring:
[System.Web.Http.HttpPost]
I now get a 404 when clicking the button:
"NetworkError: 404 Not Found - http://localhost:60282/umbraco/Api/Products/GetProductAvailability"
I've solved part of the issue somewhat by appending this:
However, I am now presented with a 400 error:
With the following response:
Are you passing in any data in your request. This error would occur if the packageCode parameter is empty or is not a integer.
Yeah I am. I'm passing a value of 100 as shown below:
Can you remove the frombody attribute from the parameter.
And update your ajax call data property like this :
data : "packagecode=" + $(this).data('tourmaster')
Dave
I've solved the issue. If you notice I am passing JSON in my Javascript request and unlike a surface controller, WebAPI seems incapable of breaking it down into its individual values so I had to define an model of sorts and pass that instead:
Nice to see you got it sorted out.
Dave
Yep. Thanks for your help!
is working on a reply...