Ajax Routing Calling Controller Name Twice

梦想的初衷 提交于 2019-12-25 02:28:17

问题


I am trying to do a simple call to my controller via ajax. The RouteConfig has not been changed and set to the default values. When I make ajax call, the Url that is requested in the "Network" debugging tools is:

'http://localhost:59275/Leaderboard/Leaderboard/GetPosition'

This is causing a 404 because the Controller, Leaderboard, is being added into the Url twice. The correct url should be

'http://localhost:59275/Leaderboard/GetPosition'

My ajax call is as follows:

  $.ajax({                
            url: 'Leaderboard/GetPosition',
            type: "GET",
            dataType: 'xml',
            success: function (data) {
                $('#results').html(data);;
            }
        });

and my controller is as follows:

public class LeaderboardController : Controller
    {
        [Webmethod]
         public static DataTable GetPosition()
         {
             // do stuff
         }


    }

回答1:


If the root URL of the page that request the ajax is "Leaderboard" then the url on the ajax call should only "GetPosition"

Or you can use "/Leaderboard/GetPosition" with "/" in front




回答2:


Use Url.Action helper for generating correct url.

Change this:

 url: 'Leaderboard/GetPosition'

to this:

 url: '@Url.Action("GetPosition","Leaderboard")'


来源:https://stackoverflow.com/questions/24388634/ajax-routing-calling-controller-name-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!