Error sending data using webapi

I have created a webapi, I am facing some problem when I post data using this api, I am very tired, can anyone help me.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>
    JSONData = {
        "CommunityID": "2",
        "QueryDescription": "gopal",
        "CategoryID": "1"

    };


    $.ajax({
        type: 'POST',
        url: '/api/querycontroller',
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(JSONData),

        success: function (data) {
            $("#button").html(data.responseText + "---------");
        },
        error: function (data) {
            $("#button").html(data.status + "---------" + data.responseText + "---------error");

        }
    });

</script>
<label id="button"></label>

      

------ my controller is below.

      [ResponseType(typeof(Query))]
    [HttpPost]
    public HttpResponseMessage Post([FromBody]Query Services)
    {
        HttpResponseMessage response = new HttpResponseMessage();

        try
        {

            Services.CommunityID = UserStatus.GetUserID(User.Identity.Name);
            Services = repository.Add(Services);

            response = Request.CreateResponse(HttpStatusCode.Created, Services);

            // string uri = Url.Route(null, new { id = Services.QueryID });

            //  response.Headers.Location = new Uri(Request.RequestUri, uri);

            return response;
        }
        catch (Exception dd)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

    }

      

when i submit without contentType: "application / json" then it shows me error: 415 --------- {"Message": "Application of request media type / x -www- form-urlencoded 'is not supported for this resource. "," ExceptionMessage ":" No MediaTypeFormatter is available to read a Request object from content with media type "application / x-www-form-urlencoded". "," ExceptionType ":" System.Net.Http .UnsupportedMediaTypeException "," StackTrace ":" in System.Net.Http.HttpContentExtensions.ReadAsAsync [T] (HttpContent content, type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable

1 formatters, IFormatterLogger formatterLogger, CancellationToken CancellationToken) "}

+3


source to share





All Articles