2

I have started some project based on ASP:NET MVC and JavaScript/jQuery. I am trying to make API call to my controller with $.ajax function, but I always get 404. I have Googled and tried a lot of different solutions, but again i get 404. I know this question is very often asked online, but again hope there won't be a lot of -1. I would appreciate any help, maybe I can't see tree because of forest.

Parameter "datas" is only string in JSON format.

My code is below:.

JavaScript call:

function checkValidId(datas) {
    $.ajax({
        url: 'api/UserInfo/Test/',
        type: 'POST',        
        data: JSON.stringify(datas),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (valid) {
            if (valid) {
                console.log("HURAAAAY!!!");
            } else {
                console.log("OOOH NO YOU DON'T !!!");
            }
        }
    });
}

My controller:

public class UserInfoController : ApiController
    {
        private DM_DBEntities1 db = new DM_DBEntities1();

        [HttpPost]
        public static string Test(string data)
        {          
            return "H E L L L L O";
        }    
    }

My WebApiConfig:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

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

2 Answers 2

1

Your configured route is wrong.

Change it to:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}"
);
Sign up to request clarification or add additional context in comments.

Comments

0

Doesn't your Test method in UserInfoController need to return ActionResult regardless of if you're returning something to be recognised as an action method?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.