but is it possible to pass a json object? Or ami going about this in the wrong way and is there a better way to pass a json object to a c# class method?
Thanks for your reply, I have actualy already read that post and it was good starting point but doesnt seem to cover posting json to a base method. I have tried posting it as follows:
namespace Base { [RestExtension("MarkerCreation")] public class TestClass { [RestExtensionMethod(returnXml = false)] public static string Hello() { string[] post = HttpContext.Current.Request.Form.AllKeys;
Oh wow, good suggerstion I am getting stuff through now! going to play with it a bit more and see if i can convert the string to Json, thanks so much Bo!
Out of curiosity any idea why it would accept strings but not Json?
I'm shootin' a bit in the dark here :-) But I think it may be due to some serializing stuff. You're posting a JSON object from javascript and grabbing it as a string array in your base method.
Also, what hit me was, when I googled your problem I found that people are writing dataType: 'jsondata' instead of dataType: 'json' in their post. Don't know if that has got something to do with anything ;-)
posting JSON object to a Base method
Hello all,
Is there a way to post a json object to a Base method? I have no problem passing simple strings to the method
http://our.umbraco.org/wiki/reference/umbraco-base/programming-a-class-for-the-base-system/umbracobase-methods-also-take-parameters
but is it possible to pass a json object? Or ami going about this in the wrong way and is there a better way to pass a json object to a c# class method?
thanks
L
Hi Lachlann,
Have a look at Sebastiaans blog about /base and JSON :-) Maybe that could be of any help?
http://cultiv.nl/blog/2010/10/12/using-base-to-create-and-consume-a-json-string
- Bo
Hi Bo,
Thanks for your reply, I have actualy already read that post and it was good starting point but doesnt seem to cover posting json to a base method. I have tried posting it as follows:
$.ajax({
type: "POST",
url: "..mybasemethod..",
data: "{ 'position': '128.3657142857143', 'markerPosition': '7' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) { alert(data); },
failure: function (errMsg) {
alert(errMsg);
}
});
with my Base method looking like:
namespace Base
{
[RestExtension("MarkerCreation")]
public class TestClass
{
[RestExtensionMethod(returnXml = false)]
public static string Hello()
{
string[] post = HttpContext.Current.Request.Form.AllKeys;
return "Received " + post.Length + " markers.";
}
}
}
but post is always empty i have tried HttpContext.Current.Request.Form["position"]
but i just get nothing :(
L
Hi Lachlann,
Out of curiousity, what happens if you post the JSON as a string instead of JSON? :-) Do you get any result back then?
- Bo
Oh wow, good suggerstion I am getting stuff through now! going to play with it a bit more and see if i can convert the string to Json, thanks so much Bo!
Out of curiosity any idea why it would accept strings but not Json?
L
Lachlann,
I'm shootin' a bit in the dark here :-) But I think it may be due to some serializing stuff. You're posting a JSON object from javascript and grabbing it as a string array in your base method.
Also, what hit me was, when I googled your problem I found that people are writing dataType: 'jsondata' instead of dataType: 'json' in their post. Don't know if that has got something to do with anything ;-)
All the best,
Bo
Very strange as I had it all working with a webservice and was just tryng to get it to use base instead
for completeness sake the last stage to get the JSON object out was to deserialize it using the Newtonsoft JSON library (http://json.codeplex.com)
List<YourClassHere> myDeserializedObjList = (List<YourClassHere>)Newtonsoft.Json.JsonConvert.DeserializeObject(post, typeof(List<YourClassHere>));
Where YourClassHere is a class description of the Json object you are expecting.
L
is working on a reply...