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?
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?
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.
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 }
);
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?
Hi,
for this to function properly in umbraco, you'd need to change the url in your ajax call to
Here's more info on using /base in umbraco.
Hope this helps.
Regards,
/Dirk
Thank you Dirk,
Is this the only way to do it? No way I can post a JSON object?
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
Nice to know it.
Thank you.
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?
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
So, Can we not pass object? ( or we need to post to a template with macro ... )
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.
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.
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...
Are you sure that work? I've Bad Request Error when POST to server.
I solved.
Client Side:
is working on a reply...