使用jquery ajax调用Controller时出错

时间:2018-01-19 14:48:25

标签: javascript jquery ajax asp.net-mvc asp.net-web-api

这是我想要调用的控制器,这个控制器位于一个解决方案中但位于另一个类库中

  [HttpGet]
        [Route("Business/GetCountries")]
        public JsonResult GetCountries(string countryName)
        {
            List<countries_model> countrylist = null;
            var myresult = "{'name':'John', 'age':31, 'city':'New York' }";
            return Json(myresult,JsonRequestBehavior.AllowGet);
        }

这是我调用控制器的jquery ajax代码

baseUrl = "/Business/GetCountries";

function getCountries()
{
    alert();
    var obj = {};
    obj.countryName = '';
    $.ajax({
        type: 'GET',
        url: baseUrl,
        dataType: 'json',
        async: false,
        success: function (data) {

            console.log(data);



        },
        error: function (err) {

            console.log(err);
        }
    });


}

这是我的路线配置

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

            routes.MapRoute(
                name: "Business",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Business", action = "GetCountries", id = UrlParameter.Optional }

0 个答案:

没有答案