none
Web API consumed by jSon RRS feed

  • Question

  • This is just killing me.

    It SHOULD be so easy.

    I've written a Web API that I need to call from json, passing arguments and getting a return value (to the json). I'm using POST because I need to pass a really long string.

    I know that my json code is calling the appropriate function because I've seen it happen in the debugger. But the parameter that is passed in is null even though I know for a fact that the json is passing a value. My code for post is returning a value that the json recognizes so the connection is right. It's just the values.

    // POST api/values
    public string Post([FromBody] string counter)
    {
        string retVal;
        retVal = "Sent [" + counter + "]"; //counter (parameter) is always null
        return retVal; //works just fine
    }

    Here is the json code.

           var counter = { counter: $('#counter').val() };

            // I've also tried $.post(uri, 'Hello World')

            $.post(uri, counter)
            .done(function (data) {
                alert('success: ' + data);
            })
            .fail(function () {
                alert('error: ' + Error);
            });

    Can anyone tell me what I'm doing wrong?

    TIA

    Rik Brooks

    • Moved by Kevin Linq Thursday, September 8, 2016 2:50 AM
    Wednesday, September 7, 2016 2:06 PM

All replies

  • Hi Rik,

    Could try your json code in this way by adding a new keyword.

    var counter = new { counter: $('#counter').val() };

    Thanks,

    Sabah Shariq

    Wednesday, September 7, 2016 2:41 PM
  • Thank you so much Sabah but that won't work. I get a JavaScript runtime error at the 'new' which says that Object doesn't support this action

    Rik


    Rik Brooks Richard.brooks@sedgwickcms.com rikbrooks@aol.com

    Wednesday, September 7, 2016 2:57 PM
  • Update, if I change the declaration to

    var counter = $('#counter').val();

    then add alert(counter) I get the right value --- but when I get to the c# code its still null

    Rik


    Rik Brooks Richard.brooks@sedgwickcms.com rikbrooks@aol.com

    Wednesday, September 7, 2016 3:15 PM
  • http://forums.asp.net/1246.aspx/1?Web+API

    You should try posting to the above forum.

    Wednesday, September 7, 2016 3:47 PM
  • Thank you, thank you.

    Rik Brooks Richard.brooks@sedgwickcms.com rikbrooks@aol.com

    Wednesday, September 7, 2016 3:48 PM
  • Hi Rikbrooks

    Thank you for posting here.

    Since your issue is related to asp.net  forum, I will move your thread to off-topic. 

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    You can post a new thread on Asp.net forum.

    Here is the link of asp.net forum:http://forums.asp.net/default.aspx/7?General+ASP+NET

    Your cooperation will be grateful.

    Best Regards,

    Kevin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


    • Edited by Kevin Linq Thursday, September 8, 2016 2:42 AM
    Thursday, September 8, 2016 2:42 AM