Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lachlann 344 posts 626 karma points
    Jun 11, 2011 @ 18:59
    Lachlann
    0

    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

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jun 13, 2011 @ 23:51
    Bo Damgaard Mortensen
    0

    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

  • Lachlann 344 posts 626 karma points
    Jun 14, 2011 @ 21:33
    Lachlann
    0

    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

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jun 14, 2011 @ 22:11
    Bo Damgaard Mortensen
    1

    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

  • Lachlann 344 posts 626 karma points
    Jun 14, 2011 @ 22:25
    Lachlann
    0

    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

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Jun 14, 2011 @ 22:39
    Bo Damgaard Mortensen
    0

    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

  • Lachlann 344 posts 626 karma points
    Jun 14, 2011 @ 23:51
    Lachlann
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft