0

Here's what I'm trying to post to my ServiceStack web service:

$.ajax({
    url: 'http://localhost:8092/profiles',
    type:'POST',
    data: {
        FirstName : "John",
        LastName : "Doe",
        Categories : [ "Category 1", "Category 2", "Category 3" ]
    },
    success: function (response) { $('#response').append('Success!'); },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status + ' Error: ' + thrownError);
      },
    dataType: "json"
});

And here's the destination class:

[Route("/profiles", "POST")]
public class ProfileRequest
{
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public string[] Categories{ get; set; }
}

All the fields are being populated except for the array. Can ServiceStack just not handle objects of this complexity or am I missing something?

Note: I've seen Unable to deserialize array which suggests using JSON.stringify() but that solution still doesn't seem to work, in fact it makes it worse by causing no data to be deserialized.

Community
  • 1
  • 1
Seth Moore
  • 3,575
  • 2
  • 23
  • 33

1 Answers1

3

ServiceStack can handle this, but your jQuery $.ajax data has to be JSON stringified using the JSON.stringify() method and set the contentType to application/json otherwise jQuery will try posting the data as application/x-www-form-urlencoded and deserialising will fail.

So your request should be:

$.ajax({
    url: 'http://localhost:8092/profiles',
    type:'POST',
    data: JSON.stringify({
        FirstName: "John",
        LastName: "Doe",
        Categories: [ "Category 1", "Category 2", "Category 3" ]
    }),
    success: function (response) { $('#response').append('Success!'); },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status + ' Error: ' + thrownError);
      },
    contentType: "application/json",
    dataType: "json"
});

Hope that helps.

Scott
  • 21,211
  • 8
  • 65
  • 72
  • Yeah, that was one of the things I had already tried, but it resulted in no data getting deserialized, not just the array. – Seth Moore Jun 23 '14 at 17:02
  • It looks like I'm using 3.9.56.0 – Seth Moore Jun 23 '14 at 17:17
  • That still doesn't seem to work. Looking at my C# objects all of the values are just null. If I remove the JSON.stringify() everything populates except for the array. – Seth Moore Jun 23 '14 at 17:23
  • @SethMoore Okay I have setup a test project and got it to work. You need to set `contentType: "application/json"` then it works. – Scott Jun 23 '14 at 18:29
  • Now that gives me the error "EndpointHandlerBase|Error occured while Processing Request: Could not find method named Options(ProfileRequest) or Any(ProfileRequest) on Service ProfileController" – Seth Moore Jun 23 '14 at 18:45
  • @SethMoore That's because you must be using CORS which you haven't configured properly. If you set your url to be `url: '/profiles'` and make the request to the same host it works. Otherwise you will need to configure CORS. – Scott Jun 23 '14 at 18:48
  • I think something must be wrong with my ajax call because I sent the stringified profile with Chrome's Advanced REST Client plugin and it worked just fine. If that's the case I'm not too worried, the ajax call was just to test the service. – Seth Moore Jun 23 '14 at 19:27