Web api Post call from ajax fails
Web api Post call from ajax fails
I have an web api wand I call it method in an ajax with POST method, sending the parameter as data . It gives me a 404 not found error.
But when I pass the data in the query string , it works fine. Please help me fixing the matter and get it worked when calling the method with data in the body, not the query string.
Note : The web application and Web-api are running on different ports
Sending a concrete type object in query string also does not work and received to web api as null
config
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, contentType, data" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
Controller
public class RiskController : ApiController
{
method
[HttpGet, HttpPost]
public DashboardContainerViewModel GetDashboardContainer(string token)
{
Ajax call
var postData = { token: priv.secToken };
$.ajax({
type: 'POST',
url: 'http://localhost:48060/api/Risk/GetDashboardContainer',
dataType: 'json',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: postData,
success: function (data) {
pub.dashboardHeader(data.Name);
var defaultTab = _.find(data.Dashboards, function (tab) {
return tab.IsDefault == true;
});
And I get POST http://localhost:48060/api/Risk/GetDashboardContainer 404 (Not Found)
Fiddler request headers
POST http://localhost:48060/api/Risk/GetDashboardContainer HTTP/1.1
Host: localhost:48060
Connection: keep-alive
Content-Length: 142
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:10452
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:10452/Authorised/DashBoard/DashBoard.aspx
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
token=ETvgg0YH4sx9w%2FiQL5560S20Ja4jGix%2FiBTOFGpQCliYZVilmtKiXPbk30d8FTYbhUWyFqz8%2FqT1pmI0oY6rzDGQ7krL6d2fDKDKwJhfCNFXZxnv%2BPCUj5ki5eizdWOM
Fiddler response headers
HTTP/1.1 404 Not Found
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcRGVmYXVsdENvbGxlY3Rpb25cQ0FNTVNfUHJvZHVjdHNcX1JlbGVhc2VcMTQxMExSLUFNXFdlYiBTZXJ2aWNlXENBTU1TLlNBQVMuV2ViQVBJXENBTU1TLlNBQVMuSVJNV2ViQVBJXGFwaVxSaXNrXEdldERhc2hib2FyZENvbnRhaW5lcg==?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, contentType, data
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Date: Fri, 29 Jun 2018 05:47:22 GMT
Content-Length: 215
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:48060/api/Risk/GetDashboardContainer'.","MessageDetail":"No action was found on the controller 'Risk' that matches the request."}
2 Answers
2
This is most lilkely due to a routing error in my opinion and experience, have you tried looking at RouteConfig.cs to make sure it sets the right routes for your API?
Something like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
If you could provide a minimal example that would be great to help you! :)
– greyhame
Jun 29 at 8:52
I have already added it, but still the same result
– Thusitha
Jun 29 at 8:53
Your Controller method uses simple type parameter. WebApi will search for simple parameter in request URL. So if you want to POST parameter, you must tell WebApi to look at the request body.
public DashboardContainerViewModel GetDashboardContainer([FromBody] string token)
But you also need to change the format of data you are sending. Simple type data sent as application/x-www-form-urlencoded
must be in format '=value'
not 'token=value'
application/x-www-form-urlencoded
'=value'
'token=value'
var postData = "=testData";
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
did you try to adding [FromBody] before the parameter, but remmber that HTTPGet doesn't have body so it will by default look at the query string.
– Hany Habib
Jun 29 at 8:22