cancel
Showing results for 
Search instead for 
Did you mean: 

Getting POST parameters for a JavaScript webscript

patorjk
Champ in-the-making
Champ in-the-making
For webscripts written in JavaScript, I'm able to get parameters in the query string via the "args" object. However, after looking over the object list in Chapter 9 of "Professional Alfresco", looking through the wiki, playing around a bit, and searching the web, I seem to be unable to find a way to get POST parameters (parameters passed in the HTTP message's body). Is there a way to access these parameters from a webscript?

As an example, here is how I'm passing the variables to the webscript from client side (this is for a Share dashlet that's talking to the repository):

var sUrl = Alfresco.constants.PROXY_URI + "folder-search";var postData = "searchTxt=" + document.getElementById("jtf_text").value;var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);‍‍‍‍
3 REPLIES 3

fstnboy
Champ on-the-rise
Champ on-the-rise
Hi,

I know it's a bit late but… Have you tried using the requestBody object?

As in requestbody.content

Check this page http://wiki.alfresco.com/wiki/Web_Scripts#Request_Processing

Hope that helps.

scouil
Star Contributor
Star Contributor
In order to use args, you must post a formdata. This can be done either with a form or with:

function getXhr(){   var xhr = null;    if(window.XMLHttpRequest)      xhr = new XMLHttpRequest();    else if(window.ActiveXObject){      try {         xhr = new ActiveXObject("Msxml2.XMLHTTP");      } catch (e) {         xhr = new ActiveXObject("Microsoft.XMLHTTP");      }   } else {      alert("${message('templates.apmSearch.get.error.XMLHTTP')}");       xhr = false;    }    return xhr;}var url = Alfresco.constants.PROXY_URI + "folder-search";var xhr = getXhr();xhr.open("POST",url,false);var data = new FormData();data.append("searchTxt", document.getElementById("jtf_text").value);xhr.send(data);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

but formdata object is not supported by ie (don't remember which versions) so there are some more obscure ways:
http://mike.kaply.com/2010/05/20/post-multipart-form-xhr/  (I don't fully understand how it works but I know it does)

zladuric
Champ on-the-rise
Champ on-the-rise
Or you can POST that as a json. Ie. from Share (or wherever) use the YUI ajax request:
Alfresco.util.Ajax.request({  url: webscriptUrl,  method: POST,  requestContentType: "application/json", … // other params‍‍‍‍‍‍

Then you can get the json object in your webscript, like this:
var param1 = json.get("param1");‍