locked
ASP.NET Web API - json/ajax parameters return "Nothing" value in Web API controller, not to one function but to json/ajax function on the same page. RRS feed

  • Question

  • User135423268 posted

    I have this wierd happening to one of my pages, I have set a json code with a PUT or POST type that calls on my WebAPI Controller

    here's the code:

        $(document).ready(function () {
            
            $('#btnSendToApprover').click(function (e) {
                debugger;
     
     
                $.ajax({
                    type: "PUT",
                    url: '/api/cbst/sendtoapprover',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify({ GetRequestNo: rn }),
                    error: function (response) {
                        alert("error");
                    },
                    success: function (response) {
                        window.location.replace('/Travel/Details?RN=' + reqno);
                    }
                });
            })
        });

    now the problem on this is when it reach the function procedure the parameter GetPONumber has a value of nothing.

        <HttpPut>
    <Route("api/cbst/sendtoapprover")>
    Public Function NewSendToApprover(<FromBody> GetRequestNo As String) As HttpResponseMessage
    Try

    And the wierdest part of this, all the post and put type on the same page of this json code also return a Nothing value on their parameters, the problem happens only on this page only but for other pages the json/ajax are fine, does anyone encounter this problem? if yes did you solve it?

    Tuesday, January 15, 2019 8:26 AM

Answers

  • User-474980206 posted

    you define the WebApi parameter as a string, but pass an object.

      {"GetRequestNo": "some value"}

    you want to pass just a string, so its just:

     $.ajax({
                    type: "PUT",
                    url: '/api/cbst/sendtoapprover',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify(rn),
                    error: function (response) {
                        alert("error");
                    },
                    success: function (response) {
                        window.location.replace('/Travel/Details?RN=' + reqno);
                    }
                });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 4:33 PM

All replies

  • User475983607 posted

    There's a couple of issues with the code.  The URL is missing a relative reference to the root /api...  The data property is just a string.  I question how you are testing because the URL cause a 404 error.  Are you showing us the code that goes with the issue?

    var getpo = "PO-00001";
    
    $.ajax({
        type: 'PUT',
        url: '/api/cbst/sendtoapprover',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(getpo),
        success: function (response) {
            //window.location.replace('/Travel/Details?PO=' + getpo);
            console.log(response);
        },
        error: function (response) {
            console.log(response);
        }
    });

    Another potential issue, a PUT updates a resources.  Generally you have an unique identifier like an ID that gets the update.  Maybe a GET is a better approach?

    Tuesday, January 15, 2019 4:12 PM
  • User135423268 posted

    Hi I modified the codes for security issues but I talked to my superior and it's okay to share the codes, here's the right one,

        $(document).ready(function () {
            
            $('#btnSendToApprover').click(function (e) {
                debugger;
    
    
                $.ajax({
                    type: "PUT",
                    url: '/api/cbst/sendtoapprover',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify({ GetRequestNo: rn }),
                    error: function (response) {
                        alert("error");
                    },
                    success: function (response) {
                        window.location.replace('/Travel/Details?RN=' + reqno);
                    }
                });
            })
        });
    
        <HttpPut>
        <Route("api/cbst/sendtoapprover")>
        Public Function NewSendToApprover(<FromBody> GetRequestNo As String) As HttpResponseMessage
            Try

    Wednesday, January 16, 2019 1:04 AM
  • User135423268 posted

    I also try it using Fiddler it also get the same problem

    Wednesday, January 16, 2019 3:05 AM
  • User36583972 posted

    Hi amendoza29,

    Form you code, you want to send a Object which contains GetRequestNo field to Web API.

    So, you need to define a object in your Web API application

    For example:

         <HttpPut>
    	Public Function PutValue(<FromBody()> ByVal value As testDoor) As HttpResponseMessage
    
    		Dim getRequestNo As String
    		getRequestNo = value.GetRequestNo
    
    
    		Dim response As HttpResponseMessage = New HttpResponseMessage()
    		response.StatusCode = HttpStatusCode.OK
    		response.ReasonPhrase = "SUCCESS"
    		response.Content = New ObjectContent(Of String)("test ok:" + getRequestNo, New JsonMediaTypeFormatter(), "application/json")
    
    		Return response
    	End Function
    
    Public Class testDoor
    	<DataMember>
    	Public Property GetRequestNo As String
    End Class

    Then, use the Ajax call the Api function:

      $(document).ready(function () {
                $("#button1").click(function () {
                    $.ajax({
                        type: "Put",
                        url: "/api/Values", //url: '/api/cbst/sendtoapprover',
                        headers: { "Accept": "application/json; odata=nometadata" },
                        contentType: "application/json; charset=urf-8",
                        dataType: "json",//222
                        data: getData(),
    
                        success: function (response) {
                            alert(response);
                        },
                        error: function () {
                            alert("Error while invoking the Web API");
                        }
                    });
                });
                function getData() {
                    var Requestinfo = new Object();
                    Requestinfo.GetRequestNo = "Yasser001";
                    return JSON.stringify(Requestinfo);
                }
            });
    

    Best Regards,

    Yong Lu

    Wednesday, January 16, 2019 7:05 AM
  • User475983607 posted

    amendoza29

    I also try it using Fiddler it also get the same problem

    Still, you did not fix the AJAX data parameter as recommended in my original post.  Pass a model to send a name/value pair.

    Wednesday, January 16, 2019 12:14 PM
  • User-474980206 posted

    you define the WebApi parameter as a string, but pass an object.

      {"GetRequestNo": "some value"}

    you want to pass just a string, so its just:

     $.ajax({
                    type: "PUT",
                    url: '/api/cbst/sendtoapprover',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    data: JSON.stringify(rn),
                    error: function (response) {
                        alert("error");
                    },
                    success: function (response) {
                        window.location.replace('/Travel/Details?RN=' + reqno);
                    }
                });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2019 4:33 PM