This is a migrated thread and some comments may be shown as answers.

Post object

3 Answers 50 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Matthew Giles
Top achievements
Rank 1
Matthew Giles asked on 22 May 2015, 05:14 AM

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!



3 Answers, 1 is accepted

Sort by
0
Matthew Giles
Top achievements
Rank 1
answered on 22 May 2015, 02:36 PM

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.

0
Erjan Gavalji
Telerik team
answered on 25 May 2015, 08:09 AM
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
0
Matthew Giles
Top achievements
Rank 1
answered on 26 May 2015, 06:59 PM
Sure, by all means convert it to a forum post, in case it helps someone else.
Tags
General Discussions
Asked by
Matthew Giles
Top achievements
Rank 1
Answers by
Matthew Giles
Top achievements
Rank 1
Erjan Gavalji
Telerik team
Share this question
or