Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

404 Not Found error when calling a Web API via jQuery

$
0
0

I have been dealing with a "404 Not Found" error when calling a Web API in an ASP.NET MVC project for some time and I just do not find the problem. This is the second Web API that I write and I had the exact same issue in the first one which was solved by including the port number in "http://localhost:port/path_to_web_api" when consuming it.  But this time it seems the situation is different. This is what I have.

The WebApiConfig Class: I commented "config.Routes.MapHttpRoute" because I am using "Route[("path_to_web_api")]" instead in each action.

public class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        //config.Routes.MapHttpRoute(
        //     name: "DefaultApi",
        //     routeTemplate: "api/{controller}/{action}/{id}",
        //     defaults: new { id = RouteParameter.Optional }
        // );

        EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);
    }
}

The simple API Controller:

[HttpGet]
[Route("api/VotoInteractiva")]
public IHttpActionResult TestAction()
{
    // Code to call DB services. For now this is a test returning Ok only.
    return Ok();
}

The Javascript code that consumes the Web API: This Javascript function is called when the user clicks a control in the View and is located directly in such a View not in a separate .js file. I included an "alert" to show the content of the "strURL" variable and it looks fine. When the AJAX call is made, the "error" part is triggered showing the 3 alerts: error, 404, Not Found. Also, I made sure my project runs in port 60642. The ïd" and "puntos" parameters of the function are not used for now.

function RegistraVoto(id, puntos) {
    var strParametro = String(puntos) + String(id);
    var strURL = encodeURI("http://localhost:60642/api/VotoInteractiva/");
    alert('strURL: ' + strURL);$.ajax
        ({
            type: "GET",
            dataType: "json",
            url: strURL,
            success: function (result) {
                alert('Resultado: ' + result)
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error');
                alert(xhr.status);
                alert(thrownError);
            }
        });
}

I have been watching, changing and testing for 2 days and I just cannot see what is wrong. I have also reviewed several similar support posts without success. I will very much appreciate any feedback.

P.D.: I am using .NET Framework 4.6.1

Respectfully,
Jorge Maldonado


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images