I'm having some issues posting a JSON object to my surface controller using ajax.
I have checked in my Firebug Net window and the JSON string is posted in the correct format however, when it gets to the method within my surface controller it is effectively interpreted as null. What am I doing wrong here as I have followed one of the examples from the Umbraco documentation almost to the letter yet 'data' always appears to be null when it reaches the GetAvailabilty method.
Javascript
$('#productavailability').click(function () {
var data = {};
data.packageCode = $(this).data('id');
$.ajax({
url: '/umbraco/surface/product/GetAvailability',
type: 'POST',
dataType: 'json',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8',
success: function (data) {
$('#availabilityResult').html(data.Availability);
}
})
});
Surface Controller
public class ProductController : SurfaceController
{
// Declare security credentials globally for use with all methods
Guid guid = new Guid(ConfigurationManager.AppSettings["axumcompanyguid"]);
String companyName = ConfigurationManager.AppSettings["axumcompanyname"];
// Setup managed user header
ManagedUserHeader managedUserHeader = new ManagedUserHeader();
// Setup compression soap header
CompressionSoapHeader compressionSoapHeader = new CompressionSoapHeader();
// Setup Web Service
TailorMadeWebServiceSoapClient ws = new TailorMadeWebServiceSoapClient();
[HttpPost]
public ActionResult GetAvailability(string data)
{
Package p = JsonConvert.DeserializeObject<Package>(data);
int test = p.packageCode;
managedUserHeader.CompanyName = companyName;
managedUserHeader.RegistrationKey = guid;
compressionSoapHeader.CompressionMethodUsed = CompressionMethods.None;
ProductAvailDataGroup[] ws_availability = null;
ws_availability = ws.GetProductAvailability(ref compressionSoapHeader, ref managedUserHeader, p.packageCode, DateTime.Today, DateTime.Today.AddDays(28));
return null;
}
}
Any help or pointers would be greatly appreciated.
Thanks for your response. Funnily enough I just worked all that out for myself.
It appears that for each parameter name in my method there needs to be a matching json key for the method to work correctly.
Passing JSON object to a surface controller
Hi all,
I'm having some issues posting a JSON object to my surface controller using ajax. I have checked in my Firebug Net window and the JSON string is posted in the correct format however, when it gets to the method within my surface controller it is effectively interpreted as null. What am I doing wrong here as I have followed one of the examples from the Umbraco documentation almost to the letter yet 'data' always appears to be null when it reaches the GetAvailabilty method.
Javascript
Surface Controller
Any help or pointers would be greatly appreciated.
You could try to create a c# object based on your json: http://json2csharp.com/
If it's called AvailabilityObject you could try to get the data like this:
The model binding of MVC should try to convert the json to the generated object.
If your json has a value called packageCode you could also try to get it like this:
Jeroen
Hi Jeroen,
Thanks for your response. Funnily enough I just worked all that out for myself. It appears that for each parameter name in my method there needs to be a matching json key for the method to work correctly.
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.