0

I am working with Alfresco Share 5.2 and currently developing new feature. Must say that I am new in Alfresco.

I need to get window location by clicking on button in js, to trigger ajax call and to get window.location.href. After that I need to send that value (url) to java backend, in my case Webscript. I managed to get url in ajax call but I do not know how to get that value in my java class.

Value is packed as JSON. I used JSON.stringify to do that.

save-search-result.post.desc.xml

<webscript>
    <shortname>Save Results</shortname>
    <description>Save Search results on button click</description>
    <url>/com/adama/gas/documents/saveResults</url>
    <format default="json" />
    <authentication runas="admin">user</authentication>
    <transaction>required</transaction>
    <lifecycle>internal</lifecycle>
</webscript>

save-search-result.post.json.ftl

{
<#if data??>
    "data" : "${data}"
</#if>
}

saveSearch.js

                     var formData = {'url': window.location.href};

                Alfresco.util.Ajax.jsonPost(
                    {
                        url: AlfConstants.PROXY_URI + "/com/adama/gas/documents/saveResults",
                        method: "POST",
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify(formData),
                        processData: false,
                        successMessage: Alfresco.util.message("Results are saved!"),
                        successCallback: {
                            fn: function (data) {
                                console.log("Data is: " + data);
                            },
                            scope: this
                        },
                        callbackScope: this
                    })

SaveSearchResultsScript

public class SaveSearchResultsScript extends DeclarativeWebScript {

    @Override
    protected Map<String, Object> executeImpl(WebScriptRequest request, Status status) {


        //how to get data from ajax call here?

        return new HashMap<>();
    }
}

Thanks in advance!

Mike1988B
  • 57
  • 5

1 Answers1

1

Your code looks good. Try this to get content from request and parse JSON.

Imports

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.InputStreamReader;
import org.springframework.extensions.surf.util.Content;

and code

Content content = request.getContent();     
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = null;
try {
    jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(content.getInputStream(), "UTF-8"));
} catch (IOException | ParseException e) {
     e.printStackTrace();
}
String data = (String) jsonObject.get("data");
David Dejmal
  • 359
  • 1
  • 8
  • Hi @David Dejmal, I added this to my code, but getting exception ` Failed to convert request to JSON`. String data in my case in null for some reason. – Mike1988B Mar 10 '23 at 10:56
  • Are you sure that you are calling the request via javascript correctly? Check it through the browser dev tools. – David Dejmal Mar 12 '23 at 19:14