javascript - Ajax call to a different controller - Stack Overflow

I am trying to call a different Controller method in an Ajax call.I have looked at different posts fo

I am trying to call a different Controller method in an Ajax call. I have looked at different posts for a solution but because I do not exactly understand what their solutions are (Links at bottom). I am calling my ajax call from my Home Controller, trying to call a method in my Production Controller.

"Request URL: http://localhost.59223/Home/Production/CreateNewProductionGoal"

This is the error I am getting.

I have tried changing the URL to .../Production/CreateNewProductionGoal and to ~/Production/CreateNewProductionGoal, as suggested online. Neither of these implementations worked because I was getting a 404 error, page not found.

One of the linked solutions mentions using a loc var key, of which I am unfamiliar.

The other suggests using a button which is not an option in my case.

            $.ajax({
            type: 'post',
            url: "Production/CreateNewProductionGoal",
            dataType: "json",
            data: data,  //Creating a ProductionGoalViewModel to pass into the CreateNewProductionGoal method
            success: function (data) {
                //$('#Dashboard').load("/Home/UpdateView/" + selectProductionLine);
            }
        });

Ajax call to different controller

How to make ajax calls to different MVC controllers

For clarification, I am looking for a step by step with explanation on how to Ajax call from one MVC controller to another.

Thanks!

I am trying to call a different Controller method in an Ajax call. I have looked at different posts for a solution but because I do not exactly understand what their solutions are (Links at bottom). I am calling my ajax call from my Home Controller, trying to call a method in my Production Controller.

"Request URL: http://localhost.59223/Home/Production/CreateNewProductionGoal"

This is the error I am getting.

I have tried changing the URL to .../Production/CreateNewProductionGoal and to ~/Production/CreateNewProductionGoal, as suggested online. Neither of these implementations worked because I was getting a 404 error, page not found.

One of the linked solutions mentions using a loc var key, of which I am unfamiliar.

The other suggests using a button which is not an option in my case.

            $.ajax({
            type: 'post',
            url: "Production/CreateNewProductionGoal",
            dataType: "json",
            data: data,  //Creating a ProductionGoalViewModel to pass into the CreateNewProductionGoal method
            success: function (data) {
                //$('#Dashboard').load("/Home/UpdateView/" + selectProductionLine);
            }
        });

Ajax call to different controller

How to make ajax calls to different MVC controllers

For clarification, I am looking for a step by step with explanation on how to Ajax call from one MVC controller to another.

Thanks!

Share Improve this question edited Jul 30, 2019 at 14:31 Dvyn Resh 9781 gold badge6 silver badges14 bronze badges asked Jul 30, 2019 at 14:29 KevinKevin 3333 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You should include a leading / in your url to call the correct URL:

$.ajax({
    ...
    url: "/Production/CreateNewProductionGoal",
    ...
});

That way, the request will go to http://localhost.59223/Production/CreateNewProductionGoal instead.

The different paths, if you currently view http://example./Home/:

  • your/path: This will take the current path as start path, i.e. and add it to there, resulting in http://example./Home/your/path.

  • ~/your/path: can be used in asp server code, e.g. Razor to indicate the root of your website, only if it rendered. Browsers do not consider ~/ a special token, resulting in http://example./Home/~/your/path or http://example./your/path, when rendered

  • .../your/path: nothing special, resulting in http://example./Home/.../your/path

  • ../your/path: go up one directory level, resulting in http://example./your/path. But this will result in different paths for nested directories.

  • /your/path: absolute path, will always result in http://example./your/path, independently of nested directories.

For more on absolute and relative URL see Absolute vs relative URLs

This might still be helpful for someone to use

$.ajax({
    ...
    url: "@string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"))/Production/CreateNewProductionGoal",
    ...
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745163614a4614516.html

相关推荐

  • javascript - Ajax call to a different controller - Stack Overflow

    I am trying to call a different Controller method in an Ajax call.I have looked at different posts fo

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信