cancel
Showing results for 
Search instead for 
Did you mean: 

Extract dataObj values ???

yascorp
Champ in-the-making
Champ in-the-making
Hello,
I have done an ajax.request to send data to a specific url

html.ftl

<javascript>
function generateForm() {

var title = new YAHOO.util.Element('${args.htmlid}-title');
var title_v = title.get('value');
var description = new YAHOO.util.Element('${args.htmlid}-description');
var description_v = description.get('value');
var category = new YAHOO.util.Element('${args.htmlid}-category');
var category_v = category.get('value');
   
var data = {               
            title : title_v,
            description : description_v,
            category : category_v
         };
        
Alfresco.util.Ajax.request(
               {   
                   url: Alfresco.constants.PROXY_URI + "api/form/add-news",
                   method: Alfresco.util.Ajax.POST,
                  dataObj:
                     {
                        site: Alfresco.constants.SITE,                        
                        dataValues: JSON.stringify(data)                        
                     },
                  
                  successCallback:                                          
                     {                     
                        fn: function generateForm_onSuccess(response) {
                           var res = response.serverResponse.responseText
                           var dataString = JSON.stringify(data);                                                   
                           console.log("Loading success !" + dataString);   
                           console.log("Loading success !" + res);                                                      
                        },
                        scope: this
                     },
                  failureCallback:
                     {
                        fn: function generateForm_onFailure(response) {
                           console.log("Loading failed …");
                        },
                        scope: this
                     },   
                  execScripts: true
               });
}
</javascript>


my add-news.post.js
<javascript>
                var siteId = json.get("site");
      
      var jsonData = json.get("dataValues");
      
      var siteRoot = siteService.getSite(siteId);
      
      logger.log("ADDNEWS : SITEID" + siteId);               
      
      // Convertir le native JS object to string
      //var objtoString = jsonUtils.toJSONString(jsonData);
      var objtoString = String(jsonData);
      model.objtoString = objtoString;
      
      logger.log("ADDNEWS : objtostring : " + model.objtoString);
</javascript>

I get this error : "json" is undefined ???

so how can I get "site" & "dataValues" VALUES ???



please help me,
thnx & regards
3 REPLIES 3

afaust
Legendary Innovator
Legendary Innovator
Hello,

for "json" to be defined you need to make sure you actually submit in JSON format and your web script is setup to handle JSON. That requires you to
<ul>
<li>set the JSON request content type - you can do that manually by setting the requestContentType parameter or instead use jsonRequest or even jsonPost (which allows you to omit the method)</li>
<li>state that your controller expects a JSON request by naming it xy.post.json.js</li>
</ul>

Also, when you submit as JSON, you do not need to manually stringify any data. YUI / Alfresco takes care of that.

Regards
Axel

yascorp
Champ in-the-making
Champ in-the-making
Thanks for your reply Axel !
I add the submitType like this :
<javascript>
……..
……..
……..
Alfresco.util.Ajax.request(
               {   
                   url: Alfresco.constants.PROXY_URI + "api/form/add-news",
                   method: Alfresco.util.Ajax.POST,
                  dataObj:
                     {
                        site: Alfresco.constants.SITE,                        
                        dataValues: data,
                        submitType: "json"                        
                     },
……….
……….
……….
</javascript>

And I rename my js like this : x.post.json.js

but json still undefined

yascorp
Champ in-the-making
Champ in-the-making
Yes, now It's working I have added requestContentType as a json, thank you so much Axel !!!
Best Regards