Post object

4 posts, 0 answers
  1. Matthew Giles
    Matthew Giles avatar
    8 posts
    Member since:
    Feb 2005

    Posted 22 May 2015 Link to this post

    Hello,

    If you look at this work item http://aspnetwebstack.codeplex.com/workitem/177 in ASP.NET Web Api, you can see that there is an issue passing arrays to a Web API method, and the workaround is to pass the array with the data encased in an object with an anonymous property, eg :



    $.ajax({
        type: 'POST',
        url: url,
        data: { '': somearray}
    }).done(function (data) {
        //do something
    }).fail({
       //handle error
    });


    In NS, when I try to do this (using content parameter of HttpRequestOptions) to post to a web api that requires the body to be an array of objects, I end up getting null being passed every time.  I can see that the content property is expecting a string, not an object and that seems to be causing the problem.



    var options: http.HttpRequestOptions = {
        url: url,
        method: "POST",
        content: { "": somearray }
    };
    http.request(options).then(function (response) {
        //do something
    }, function (error) {
        //handle error
    });


    Do you have any suggestions?



    Thanks!



  2. Matthew Giles
    Matthew Giles avatar
    8 posts
    Member since:
    Feb 2005

    Posted 22 May 2015 Link to this post

    I have found a workaround to this issue.  I narrowed it down using just a string, rather than an array, and compared what was being sent from the mobile app to what was being sent from MVC project, using Fiddler.  It showed that using the data: { '', somestring } syntax in the request was prepending an "=" character to the body of the request, which was apparently necessary for the Web API routing to bind it to the parameter of the API method when using the [FromBody] attribute.  Doing the same in NS with content: {'', somestring} was just sending [object Object] (even when the data was already stringified).  I manually prepended an "=" character to my stringified data in the NS project, and then I could just send it as content: somestring without needing to wrap it.  So the moral of this story seems to be, add an "=" character to the content in NS if you want it to be a parameter to a Web API call.

  3. Erjan Gavalji
    Admin
    Erjan Gavalji avatar
    1455 posts

    Posted 25 May 2015 Link to this post

    Hi Matthew,

    Indeed, as you found it yourself, we don't have any serialization implemented in the http module in an effort to keep the things as simple as possible. Thus, the NativeScript framework is not locked to a specific client-server communication implementation.

    Would you like to have this support ticket transferred to a forum post, so that the community benefits it when the same problem hit?

    Kind regards,
    Erjan Gavalji
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  4. Matthew Giles
    Matthew Giles avatar
    8 posts
    Member since:
    Feb 2005

    Posted 26 May 2015 Link to this post

    Sure, by all means convert it to a forum post, in case it helps someone else.
Back to Top