Complex Types in Method Signature

Oct 12, 2011 at 12:54 PM
Edited Oct 12, 2011 at 12:56 PM

Does RestCake support doing something like the following:

 [OperationContract]
 [Post(UriTemplate = "ServiceMethod")]
 public object SomeMethod(SomeRequest someRequest)
 {
	... //some operation in here
 }

[DataContract]
public class SomeRequest
{
	[DataMember]
	public string Property1{ get; set; }

	[DataMember]
	public string Property2{ get; set; }
}
And then do this from jQuery:

var command = { Property1: $('#field1').val(), Property2: $('#field2').val() };

  $.ajax({
       type: "POST",
       url: 'http://localhost/services/ServiceMethod',
       data: command,
       success: function (value) {
            showMessage("Success");
       },
       error: function (xhr, status, error) {
            if (status == "error") {
		showError(($.parseJSON(xhr.responseText)));
            }
       }
 });

 

I keep getting errors from the handler...

Oct 12, 2011 at 1:00 PM
Edited Oct 12, 2011 at 4:10 PM

I've figured this out - maybe helpful to some others.  You have to do the following in jQuery -

 

 

  var contact = new Object();                    
contact.Property1= "Jesper";                    
contact.Property2= "Aaberg";
         
var command = JSON.stringify(contact);           
$.ajax({
                        type: "POST",
                        url: 'http://localhost/services/ServiceMethod',

 

 

                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: command,
                        success: function (value) {
                           
                            showMessage("Success");
                        },
                        error: function (xhr, status, error) {
                            if (status == "error") {

                                showError(($.parseJSON(xhr.responseText)));
                            }
                        }
                    });