How do I send an array to servicestack with javascript?

1,092 views
Skip to first unread message

mtrax

unread,
Mar 10, 2012, 4:32:50 PM3/10/12
to servic...@googlegroups.com
Hi everybody!

I'm using this wonderful framework for many months and I'm really happy with it and I like it, but now I'm stucking at problem. This my pseudo code and below that my problem description.

Javascript:

var data = new Array();

var propertyChanged = {
"Property" : "Name",
"FromValue": "Alex",
"ToValue": "Peter",
"Date": "2012-01-01 00:00:000"
};

data.push(propertyChanged);

    $.ajax({
        type: 'POST',
        conentType: 'application/json; charset=utf-8',
        data: data,
        url: url,
        success: function (xhr, status) {

        },
        error: function (xhr, status) {

        }
    });

C#:

[DataContract]
public class PropertyChangeRequest
{
[DataMember]
public List<PropertyChanged> PropertiesChanged { get; set; } 
}

public class PropertyChanged
{
public string PropertyName { get; set; }
public DateTime Date { get; set; }
public string FromValue { get; set; }
public string ToValue { get; set; }
}

My problem ist, that PropertyChangedRequest is everytime NULL, after my request. Can somebody tell me what's wrong with my code?

Thanks in advance!

greets mario

Demis Bellot

unread,
Mar 10, 2012, 4:51:01 PM3/10/12
to servic...@googlegroups.com
Hi,

Is your ServiceStack expecting camelCase? i.e. when you call a JSON service does it return the properties in camelCase?


mtrax

unread,
Mar 10, 2012, 5:15:10 PM3/10/12
to servic...@googlegroups.com
I'm not sure what you exactly mean, but my properties are all in PascalCase, like in C#.  If I add a simple string property to my request everything works for this "simple" property fine. I've problem with arrays or lists as property. 

Sorry, I made a mistake, I meant that PropertiesChanged property is null, not the PropertiesChangedRequest itself.

Demis Bellot

unread,
Mar 10, 2012, 5:16:33 PM3/10/12
to servic...@googlegroups.com
What happens when you call a service with ?format=json in your web browser do you get back PascalCase or camelCase properties in the response?


On Sat, Mar 10, 2012 at 5:15 PM, mtrax <brai...@gmail.com> wrote:
I'm not sure what you exactly mean, but my properties are all in PascalCase, like in C#.  If I add a simple string property to my request everything works for this "simple" property fine. I've problem with arrays or lists as property. 

Sorry, I made a mistake, I meant that PropertiesChanged property is null, not the PropertiesChangedRequest itself.



mtrax

unread,
Mar 10, 2012, 5:24:58 PM3/10/12
to servic...@googlegroups.com
PascalCase

Demis Bellot

unread,
Mar 10, 2012, 5:30:30 PM3/10/12
to servic...@googlegroups.com
Right can I see the wire format of the JSON you're sending to ServiceStack? this is really what you need to know what's going wrong.
You can get it from the network tab of Chrome.


On Sat, Mar 10, 2012 at 5:24 PM, mtrax <brai...@gmail.com> wrote:
PascalCase

Arxisos

unread,
Mar 10, 2012, 5:35:50 PM3/10/12
to servic...@googlegroups.com
Hi, 

I think you've got a mistake in the client code:

var data = new Array();

var propertyChanged = {
"Property" : "Name",
"FromValue": "Alex",
"ToValue": "Peter",
"Date": "2012-01-01 00:00:000"
};

data.push(propertyChanged);

    $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: { PropertiesChanged: data },

        url: url,
        success: function (xhr, status) {

        },
        error: function (xhr, status) {

        }
    });

Demis Bellot

unread,
Mar 10, 2012, 5:41:51 PM3/10/12
to servic...@googlegroups.com
Good catch :)

mtrax

unread,
Mar 10, 2012, 6:12:44 PM3/10/12
to servic...@googlegroups.com
Oh! Amazing eyes Arxisos! Thanks man!

I will change this, give a try and response of course... 


mtrax

unread,
Mar 10, 2012, 8:18:11 PM3/10/12
to servic...@googlegroups.com
Yes! It was the missing "t" in contentType. Now everything works fine!

Thanks guys!
Reply all
Reply to author
Forward
0 new messages