0

I have a service contract with a method as follows:

[OperationContract]
    [WebInvoke(Method = "POST",
                               RequestFormat = WebMessageFormat.Json,
                               ResponseFormat = WebMessageFormat.Json,
                               BodyStyle = WebMessageBodyStyle.Bare,
                               UriTemplate = "FibonacciNumber/")]
    long FibonacciNumber(long n);



public long FibonacciNumber(long n)
    {
        var a = 0;
        var b = 0;

        for(var i = 0; i <= n; i++)
        {
            if(i == n)
            {
                return a;
            }

            var c = a;
            a = b;
            b = b + c;
        }

        return 0;
    }

However, when I try to call the above with a jquery ajax call, the value of the parameter "n" is 0 when the FibonacciNumber service gets hit, any idea what may be wrong? I did console log the value of "$('#fibIndex').val()" and it shows the correct value on the client:

$('#btnFib').on('click', function () {
        fibIndex = $('#fibIndex').val();

        $.ajax({
            url: "http://localhost:61924/MyService.svc/FibonacciNumber/",
            type: "POST",
            dataType: "json",
            data: Number(fibIndex),
        }).done(function (data) {
            $('#fibResult').html(data);
        }).fail(function () {
            console.log("error");
        });
    });

Thank you in advance for shedding some light in this regards.

Zen Pak
  • 578
  • 6
  • 22
  • Is `fibIndex ` declared globaly ? – Mairaj Ahmad Jun 11 '16 at 07:32
  • Thanks for asking, this is a typo, var is meant to be in front of "fibIndex". This means declared locally, still not working – Zen Pak Jun 11 '16 at 07:58
  • In browser console check netwrok tab to see actually what value is being passed to the service ? – Mairaj Ahmad Jun 11 '16 at 08:03
  • Hi Mairaj, the request headers are: Accept:application/json, text/javascript, */*; q=0.01 Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Connection:keep-alive Content-Length:0 Host:localhost:61924 Origin:http://localhost:63479 Referer:http://localhost:63479/Home/Index User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36 – Zen Pak Jun 11 '16 at 08:07
  • Here you will see Request url and data plese share that. – Mairaj Ahmad Jun 11 '16 at 08:08
  • content length is 0 in the request but the response is 1. – Zen Pak Jun 11 '16 at 08:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114410/discussion-between-mairaj-ahmad-and-zen-pak). – Mairaj Ahmad Jun 11 '16 at 08:10
  • data: { n :Number(fibIndex)}, http://stackoverflow.com/questions/2002163/jquery-ajax-call-data-parameters-are-not-being-passed-to-mvc-controller-action – Cleriston Jun 11 '16 at 09:44
  • changed it as per above but get a 400 bad request. The expected request data is:The following is an example request Json body: 9223372036854775807 – Zen Pak Jun 11 '16 at 10:09

1 Answers1

0

I found the issue with my code. The problem was that I was missing the contentType and not passing the correct data. The correct example is as follows:

$('#btnFib').on('click', function () {
        var fibIndex = Number($('#fibIndex').val());

        $.ajax({
            url: "http://localhost:61924/MyService.svc/FibonacciNumber/",
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(fibIndex),
        }).done(function (data) {
            $('#fibResult').html(data);
        }).fail(function (a) {
            console.log(a);
        });
    });
Zen Pak
  • 578
  • 6
  • 22