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?
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.
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:
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?
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:
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
They are null
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?
Found it :)
contentType: "application/json; charset=utf-8",
From jQuery.com:
contentType String
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
:)
Ahh, right on! :-)
is working on a reply...