Copied to clipboard

Flag this post as spam?

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


  • Anders Aleborg 35 posts 67 karma points
    May 11, 2012 @ 19:46
    Anders Aleborg
    0

    Posting to /base

    Hi,

    I have a small issue, I've created some RestExtensionMethods in /base and GET works fine but I need to post a form and using /param1/param2 etc isn't enough.

    Code looks like:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using umbraco.presentation.umbracobase;
    
    namespace GenericApi
    {
        [RestExtension("MyApi")]
        public class MyApi
        {
            [RestExtensionMethod()]
            public static string MyPostFunction()
            {
                string[] post = HttpContext.Current.Request.Form.AllKeys;
                return JsonConvert.SerializeObject(post);
            }
        }
    } 

    All I get in return is [], in other words Request.Form is empty.
    I've tried to make a post from a regular form and by $.ajax(....)

    Any ideas why I don't seem to be able to retrive the post data?

  • Anders Aleborg 35 posts 67 karma points
    May 11, 2012 @ 20:07
    Anders Aleborg
    0

    The regular POST in a form works, just had forgotten to sate the "name" parameter in all inputs but it's not working with a simple jquery test:

       function submitMyData() {
            $.ajax({
                type: "POST",
                async: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: {param1:'test1',param2:'test2'},
                url: '/base/MyApi/MyPostFunction',
                success: function (response) {
                },
                error: function (response2) {
                    alert(response2);
                }
            });
        }
    
    
  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 11, 2012 @ 22:01
    Bo Damgaard Mortensen
    0

    Hi Anders,

    Out of curiousity, what happens if you try to grap the data in your base method like this:

    HttpContext.Current.Request["param1"] and HttpContext.Current.Request["param2"] ?

    All the best,

    Bo

  • Anders Aleborg 35 posts 67 karma points
    May 11, 2012 @ 22:18
    Anders Aleborg
    0

    They are null

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 11, 2012 @ 22:27
    Bo Damgaard Mortensen
    0

    Alright,

    Then, what if you do a simple jQuery post, like this:

    $.post(yourBaseUrl, { param1: "param1", param2: "param2" }, function callback(data) {

        alert(data);

    });

    And then fetch the params like I described in my latest post?

  • Anders Aleborg 35 posts 67 karma points
    May 11, 2012 @ 22:32
    Anders Aleborg
    1

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

    From jQuery.com:

    contentType               String
    Default: 'application/x-www-form-urlencoded'

    When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases. If you explicitly pass in a content-type to $.ajax() then it'll always be sent to the server (even if no data is sent). Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side.

    Seems like .NET doesn't like application/json
    :)

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    May 11, 2012 @ 22:35
    Bo Damgaard Mortensen
    0

    Ahh, right on! :-) 

Please Sign in or register to post replies

Write your reply to:

Draft