Copied to clipboard

Flag this post as spam?

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


  • Ben 91 posts 111 karma points
    Aug 14, 2011 @ 12:12
    Ben
    0

    post a json object to a /base function

    When I post a JSON object to a regular asp.net page method, I can do it like this:

     

    ***client***

    function send() {

                var myData = '{"title":' + '"' + $("#tiltle-text").val() + '",' +

                    '"content":' + '"' + $("#content-text").val() + '"' + 

                     '}';

                alert(myData);

                var loc = window.location.href;

                $.ajax({ 

                    type: "POST",

                    url: loc + "/WriteToFile",

                    data: myData,

                    contentType: "application/json; charset=utf-8",

                    dataType: "json",

                    success: function (data) { alert("success"); },

                    fail: function (data) { alert("failed"); }

                });

     

            }

    ***server***

     

    [WebMethod]

        public static void WriteToFile(string title, string content)

        {

            StreamWriter sw = new StreamWriter(@"c:/ben.txt");

            sw.WriteLine(title);

            sw.WriteLine(content);

            sw.Close();

        }

    When I try to do the same with a /base function. I get the error: <error>Not Enough parameters in url</error>

    what's wrong?

     

     

     

     

     

     

     

     

     

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 14, 2011 @ 14:09
    Dirk De Grave
    0

    Hi,

    for this to function properly in umbraco, you'd need to change the url in your ajax call to


    url: loc + '/WriteToFile/' + $("#tiltle-text").val() + '/' +  $("#content-text").val()


    Here's more info on using /base in umbraco.

    Hope this helps.
    Regards,
    /Dirk 

  • Ben 91 posts 111 karma points
    Aug 14, 2011 @ 17:38
    Ben
    0

    Thank you Dirk,

    Is this the only way to do it? No way I can post a JSON object?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 14, 2011 @ 18:33
    Dirk De Grave
    0

    Ah, nope, that's just how umbraco base is working. Btw, did you know you can decorate your method with a RestExtensionMethod attribute and your class with a RestExtension attribute, making configuration obsolete?

     

    Cheers,

    /Dirk

  • Ben 91 posts 111 karma points
    Aug 15, 2011 @ 09:19
    Ben
    0

    Nice to know it.

    Thank you.

  • kaushal 5 posts 25 karma points
    Aug 19, 2011 @ 06:48
    kaushal
    0

    Hi Dirk, If i have a large amount of data for say $("#content-text").val() in string, this wont get submitted because the url will get heavy.
    Any solution for this? 

  • Don Ross 28 posts 49 karma points
    Aug 19, 2011 @ 12:52
    Don Ross
    0

    Absolutely you can post JSON data to a /base url as a POST variable. In your code behind you can get the data from Request.Form[0], or whatever you called your passed in parameter. I find using Newtonsoft to parse the JSON quite nice. We have been using JSON with the Umbraco base for quite some time with great success.

     EDIT: BTW, you don't need to pass it in as a paramter to your base function, therefore not getting the error 'not enough parameters passed'. When using a rest extension, any parameters you have in your function is required to have that same amount of parameters on your URL.

    Don

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    May 04, 2012 @ 16:04
    Biagio Paruolo
    0

    So, Can we not pass object? ( or we need to post to a template with macro ... )

  • Don Ross 28 posts 49 karma points
    May 04, 2012 @ 18:25
    Don Ross
    0

    You cannot pass an object, but you can convert your object to a string and pass it to the server where you will process it.

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    May 04, 2012 @ 19:05
    Biagio Paruolo
    0

    Ok, but How serialize object in string with jquery?

    As someone post up, it's possible to post object to /base/service and read parameters as Request.form. I have not try, yet.

     

  • Don Ross 28 posts 49 karma points
    May 04, 2012 @ 19:17
    Don Ross
    0

    You could use the json2 class and do a JSON.stringify(jsonObj) to convert a JSON object to a string and pass to server as post variable. Then on server you can use Newtonsoft to convert posted string back to JSON object. Also you can using Newtonsoft to create JSON to send to the client as below...

     

    ArrayList objs = new ArrayList ();
    MembershipUser mu = Membership.GetUser (); 
    MemberProfile mp = MemberProfile.GetUserProfile ( mu.UserName ); 
    
    objs.Add ( new 
    { firstName = mp.firstName, 
    lastName = mp.lastName, 
    address1 = mp.address, 
    address2 = mp.address2, 
    city = mp.city, 
    state = mp.state, 
    zip = mp.postalCode, 
    homePhone = mp.homePhone, 
    mobilePhone = mp.mobilePhone, 
    emailAddress = mu.Email } 
    ); 
    
    return JsonConvert.SerializeObject ( objs );
    
  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    May 07, 2012 @ 10:07
    Biagio Paruolo
    0

    Are you sure that work? I've Bad Request Error when POST to server.

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    May 08, 2012 @ 11:49
    Biagio Paruolo
    0

    I solved.

    Client Side:

    orderArray.push(readObj);
                    //wrap in object  
                wrap = orderArray;
                var flag;
                alert(JSON.stringify(wrap));
    
                $.ajax({
                    type: "GET",
                   url: "/base/Class/SaveData.aspx",
                    data: JSON.stringify(wrap),
                    processData: false,
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data == "1") {
                            alert("Ok!");
                            flag = 1;
                        } else {  
                            flag = 0;
                            alert("Non Ok!");
                        }
                    },
                    error: function (e) {
                        $("span").text("Not valid!" + e.message).show().fadeOut(1000);
                        flag = 0;
                        alert("Error " + e.message);
                    }
                });
                if (!(flag))
                    return false;
    Server Side:
    [RestExtensionMethod(returnXml = false)]
            public static string SaveData()
            {
     string qs = HttpContext.Current.Request.QueryString[0];
                Object obj = JsonConvert.DeserializeObject(qs);
                var jarray = JsonConvert.DeserializeObject<List<DataRead>>(qs);
    
                json = JsonConvert.SerializeObject(obj, Formatting.Indented);
                          if ( obj != null )
                { ......
     Bye Bye
     
     
Please Sign in or register to post replies

Write your reply to:

Draft