简体   繁体   English

Javascript Ajax调用无法访问我的Web API控制器页面。 似乎无法正确获得网址

[英]Javascript ajax call can't hit my Web API controller page. Can't seem to get the url correct

I have a registration page that I'm trying to save the input fields to a new record in my Users table. 我有一个注册页面,我试图将输入字段保存到Users表中的新记录中。

 <button class="btn-u" type="submit" 
 onclick="submitclicked()">Register</button>

the click is picked up and I can get breakpoints hit on my .js 点击被拾起,我可以在我的.js上找到断点

function submitclicked() {
    insertNewUser();

    ko.applyBindings({ insertNewUser: insertNewUser });
};

function Person() {
    this.FirstName = firstname();
    this.LastName = lastname();
    this.Email = email();
    this.Password = password();
}

var insertNewUser = function () {

    var person = new Person();
    var url = "/api/Home/addUser"
    $.ajax({
        url: url,
        type: 'post',
        data: person,
        contentType: "application/json; charset=utf-8",
        success: function (result) {
        }
    })
};

I also have a breakpoint within my RegController.cs file but it doesn't ever get hit. 我的RegController.cs文件中也有一个断点,但是它从未被命中。 this leads me to believe that it is never getting into my controller. 这使我相信它永远不会进入我的控制器。 My controller looks like this: 我的控制器如下所示:

public class RegController : ApiController
{
   //now insert  these values into the DB
    private static void addUser(Person item)
    {
        var db = new MyEntities();

        try
        {
            User record = new User()
            {

                First_Name = item.FirstName,
                Last_Name = item.LastName,
                Email = item.Email,
                Password = item.Password
            };
            db.User.Add(record);
            db.SaveChanges();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.InnerException);
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
    }

}

In the solution explorer it goes RegController.cs > RegController > Person > addUser(Person): void. 在解决方案资源管理器中,它进入RegController.cs> RegController> Person> addUser(Person):无效。

I've tried every different option I could think of for the ajax url with no success. 我已经尝试过我能想到的针对ajax url的所有其他选项,但均未成功。 Can someone show me what I've done wrong? 有人可以告诉我我做错了什么吗?

EDIT: Adding RouteConfig 编辑:添加RouteConfig

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

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

There are couple of issues: 有几个问题:

  1. The action in your controller is static. 控制器中的动作是静态的。

  2. The action in your controller is private and not public. 控制器中的操作是私有的而不是公开的。

  3. The route you are using is pointing to the wrong endpoint (use the route debugger or API help to see the correct one). 您使用的路由指向错误的端点(使用路由调试器或API帮助来查看正确的端点)。

Some updates to consider: 需要考虑的一些更新:

public class RegController : ApiController
{
   //now insert  these values into the DB
    public void addUser(Person item)
    {
            ...

    }

    public class Person
   {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
    }

}

For the jquery request: 对于jquery请求:

var insertNewUser = function () {

    var person = new Person();
    var url = "/api/Reg"
    $.ajax({
        url: url,
       type: 'post',
        data: person,
        contentType: "application/json; charset=utf-8",
        success: function (result) {
        }
    })
};

Assuming this is a vanilla web api v2 application, if you run your web application and click the API link, it will show you how your routes are configured. 假设这是一个普通的Web api v2应用程序,如果您运行Web应用程序并单击API链接,它将向您显示如何配置路由。

Side note: I find it useful when debugging web api to utilize the following package: 旁注:在调试Web api以利用以下包时,我发现它很有用:

Install-Package WebApiRouteDebugger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM