最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass a single parameter to WebApi controller using jQuery Ajax - Stack Overflow

programmeradmin3浏览0评论

I am trying to pass a single parameter (string value) to webapi controller but, it is not working. The parameter value is reaching the controller as 'null'. Here is my WebApi config,

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{action}"
           );

        }
    }

Controller:

        [HttpPost]
        public HttpResponseMessage GetDataView(string request)
        { 
            try
            {
                var result = DB.GetDataView(request);

                return Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (Exception ex)
            {            
                //todo: log exception   
                throw ex;
            }
        }

AJAX

var serverName = 'ds100';
 $.ajax({
        url: 'api/ServerInfo/GetDataView',
        type: 'POST',
        dataType: 'json',
        data: serverName,
        success: function (data, textStatus, xhr) {

        },
        error: function (xhr, textStatus, errorThrown) {

        }

Am I missing anything? Any help is appreciated.

I am trying to pass a single parameter (string value) to webapi controller but, it is not working. The parameter value is reaching the controller as 'null'. Here is my WebApi config,

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{action}"
           );

        }
    }

Controller:

        [HttpPost]
        public HttpResponseMessage GetDataView(string request)
        { 
            try
            {
                var result = DB.GetDataView(request);

                return Request.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (Exception ex)
            {            
                //todo: log exception   
                throw ex;
            }
        }

AJAX

var serverName = 'ds100';
 $.ajax({
        url: 'api/ServerInfo/GetDataView',
        type: 'POST',
        dataType: 'json',
        data: serverName,
        success: function (data, textStatus, xhr) {

        },
        error: function (xhr, textStatus, errorThrown) {

        }

Am I missing anything? Any help is appreciated.

Share Improve this question asked May 28, 2015 at 18:54 JGVJGV 5,1979 gold badges56 silver badges101 bronze badges 2
  • 1 you can try this data: {request: serverName} at your ajax method – Mahedi Sabuj Commented May 28, 2015 at 18:59
  • You get a an error: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' – Si8 Commented Jan 25, 2017 at 19:01
Add a ment  | 

1 Answer 1

Reset to default 4

The issue seems to be on you ajax parameters the data parameter receives an object (json) that holds a property for each of the values you are passing in the request, I think you should use

 var serverName = 'ds100';
 $.ajax({
    url: 'api/ServerInfo/GetDataView',
    type: 'POST',
    dataType: 'json',
    data: {request: serverName} ,
    success: function (data, textStatus, xhr) {

    },
    error: function (xhr, textStatus, errorThrown) {

    }

and that should do it

发布评论

评论列表(0)

  1. 暂无评论