1

I've spent over a day just trying to get the jquery of my home page, to hit an api controller I have in my C# ASP.NET MVC project.

The RouteConfig.cs in APP_START is as such:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "API Default",
            url: "api/{controller}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );


    }

My class in called LogInController and is in the Controllers directory: Project Structure

I've got the LogInController class set up as such: LogInController Structure

Here is my jquery on Index.cshtml:

var apiUrl = "api/LogIn";

-

    $.post(apiUrl,
            {
                userName: 'kamron',
                pwHash: sha256_digest('password')

            },
            function (data, status) {
                try {
                    alert(data, status);
                } catch (e) {
                    alert("System error, please try again.");
                };
            });

and

$.getJSON(apiUrl)
    .done(function (data) {
        // On success, 'data' contains a list of products.
        $.each(data, function (key, item) {
            // Add a list item for the product.
            alert(key + ':' + item);
        });
    });

I would appreciate anything and everything. I've been staring at this for over a day now trying to figure out what my problem is....

Any extra info that would help in helping me please ask for it, I tried to post everything relevant.

Thanks alot everyone :)

Community
  • 1
  • 1
Kamron K.
  • 594
  • 1
  • 10
  • 18
  • 1
    Put you `API Default` route before the `Default` route –  Feb 02 '15 at 02:08
  • Ok, did that, but still getting 404's on both the GET and the POST – Kamron K. Feb 02 '15 at 02:44
  • Try `var apiUrl = "/api/LogIn";` –  Feb 02 '15 at 02:48
  • No luck :/ I appreciate the help. When defining a new ApiController in an MVC project, is there anywhere I need to do anything? I didn't see anything in the Web.config that seemed relevant – Kamron K. Feb 02 '15 at 02:51
  • Sorry, I didn't read you code carefully enough - [this answer](http://stackoverflow.com/questions/18420878/asp-net-mvc-4-apicontroller-and-normal-controller-routes) may help –  Feb 02 '15 at 02:56

1 Answers1

1

You don't seem to have an action on the controller accepting a POST. Also, the $.getJSON call doesn't have any arguments attached to it and you typically wouldn't want to pass credentials via a GET request for security reasons.

Posting multiple arguments to a WebApi is best achieved by creating a model representing the parameters, i.e,

 public class LogonRequest
 {
      public string Username { get; set; }
      public string Password { get; set; }
 }

and then accepting the model as shown below:

 // POST api/values
 public string Post([FromBody]LogonRequest creds)                          
 {
      //auth code...
 }

A good example of setting up both the jQuery call and the POST action can be found here: How to pass json POST data to Web API method as object

Also, Fiddler can be a great help with these issues if you're not already using it.

Community
  • 1
  • 1
JTW
  • 3,546
  • 8
  • 35
  • 49
  • The linked question above in the comments of my OQ, and this answer right here lead to me solving my issue :D – Kamron K. Feb 02 '15 at 03:32