Jquery AJAX Result data retrieve back to client side in velocity template

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2013

I have just created an AJAX request in velocity template and able to get request at .java file as below:

var url ="PlanIssuesAction!IssuesPlanning.jspa"; jQuery.post(url,myJSONObject,function(result){ alert('success');}).done(function(){ alert("in done");}).fail(function(){ alert("error");}).always(function(){ alert("finished");});

On the server side, in doIssuesPlanning method, able to get call and insert the posted data.

Now I need to return the result data to the client side back at jquery "Result" parameter. How can I achieve this? Currently, in "Result" object shows all HTML text.

Thank you.

3 answers

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

ignore my previous answers and try something like this

set the message in your java file as

addErrorMessage("Success executed result appear");

add something like this in ur velocity file

#foreach ($error in $action.getErrorMessages())
                            <li>$error</li>
                            #end
                            #foreach ($error in $action.errors)
                            <li>$error</li>
                            #end

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

Do i have to add as below at server side ?:

   @POST
	    @Path("/save")
	    @Produces("application/json")
	    //@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	    public String SaveIssuesPlanning()
	    {
	    	addErrorMessage("Success executed result appear"); //added newly
	    	
	    	logger.info("in saveissuesplanning method");
	    	logger.info(end + start  + issues);
	    	logger.info("end saveissueplanning");
	    	return "result success found";
	    }

At client side, in error event have to add as below ?

  error: function(resp, status, xhr){
             /*added new block */
                #foreach ($error in $action.getErrorMessages())
                            <li>$error</li>
                            #end
                            #foreach ($error in $action.errors)
                            <li>$error</li>
                            #end
                /* end added new block */
                	alert('in error json');  
                    alert("Error: " + resp.e); 
                    jQuery("#errorPost").html("Error: " + resp.e  + " - STATUS: " + xhr.status + " " + xhr.statusText);

Please correct me... and also let me know , how should i keep URL for calling method at server side.

RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

use your url as

var url ="PlanIssuesAction!IssuesPlanning.jspa"; jQuery.post(url,myJSONObject,function(result){ alert('success');}).done(function(){ alert("in done");}).fail(function(){ alert("error");}).always(function(){ alert("finished");});

as you said On the server side, in doIssuesPlanning method, able to get call and insert the posted data and after that addthis method.

addErrorMessage("Success executed result appear");
in vm file add this
#foreach ($error in $action.getErrorMessages())
<li>$error</li>
#end
#foreach ($error in $action.errors)
<li>$error</li>
#end
dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

in vm file, error event , alert shows me "undefined",

alert("Error: " + resp.e); alert("Error: " + xhr.statusText); alert("Error: " + xhr.status);


dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

ok. i have undo as above.

and added addErrorMessage("Success executed result appear"); line at server side at end of the function as below:

public String doIssuesPlanning() 
{
logger.info("in PlanIssuesAction");

...

addErrorMessage("Success executed result appear");
	    	
	        return getRedirect("PlanIssuesAction!default.jspa");
	    }

In velocity template,

added

JQuery.post(url,myJSONObject,function(result)
{
           							alert(result);
})
.done(function(result) { alert("second success" + result); })
.fail(function() { alert("error event in JQUERY AJAX"); 


#foreach ($error in $action.getErrorMessages())
&lt;li&gt;$error&lt;/li&gt;
alert("error as " + $error);
#end
#foreach ($error in $action.errors)
alert("error is " + $error);
&lt;li&gt;$error&lt;/li&gt;
#end

})
.always(function() { alert("finished");   selectedVal = ''; }
);

No any effect looks like. when add alert for " $action.getErrorMessages()" then shows empty message box.

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

On server side, if i keep as below: then, on client side, .done- alert (result) messagebox will show me whole HTML page's code and no error raise. But problem remains there as it is , how to send result data to client side.

//addErrorMessage("Success executed result appear");  //if uncomment then, ajax event catch in error. 	

return getRedirect("PlanIssuesAction!default.jspa");

Alternate try,

If i update above stuff as below: then, client side, ajax event will catch at - .Fail and error raised instead of .done result capature.

//addErrorMessage("Success executed result appear"); //commented    	
// return getRedirect("PlanIssuesAction!default.jspa"); //commented

   return "final data result from AJAX call - success"; //result data to capture at client side.

Another :

if it could be not be possible straightly as it looks, then, instead of JiraWebActionSupport java file, do you think it could achievable through HttpServlet?

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2013

this is what i am using at server side

@GET
	@AnonymousAllowed
	@Path("/test")
	@Produces("application/json")
	public Response getMajorSystems(@QueryParam("ProjectId") String projectId) {
	JSONObject json = new JSONObject();
//add your logic here
	json.append("status","Sucess");
	
	
	return Response.ok(json.toString()).build();

	}

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

Here, at server side, i have been using "JiraWebActionSupport" and not "HttpServlet", so, unable to get "Response" object ., is there any alternative to use ?

Also, please let me know, how this particular "getMajorSystems" method will be called from client side AJAX url ?, is it like - var url = "PlanIssuesAction!IssuesPlanning.jspa"; (keep - doIssuesPlanning() method) ?

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

Hi

Thanks for your comments.

Based on your comments, i have created below stuff but , it every time, goes to error only and unable to call server sider method. Can you please look at below and correct me.

client side in velocity template:

var url ="http://localhost:2990/jira/secure/PlanIssuesAction!default.jspa/SaveIssuesPlanning/save";

 

//(also tried below possibilities of url but not worked),

//var url ="http://localhost:2990/jira/secure/PlanIssuesAction!default.jspa/SaveIssuesPlanning";

//var url ="http://localhost:2990/jira/secure/PlanIssuesAction!default.jspa/save";

 

jQuery.ajax({

                type: "POST",

                url: url,

                contentType: "application/json",

                dataType: "json",

                data:  "SaveRequestingData",

                success: function (resp, status, xhr) {

               alert('in success JSON');

                    var msg = "result is: " + resp.result + ", Start: " + resp.start + ", End: " + resp.end + ", issues: " + resp.issues;

                    alert(msg);

                    jQuery("#successPost").html(msg  + " - STATUS: " + xhr.status + " " + xhr.statusText);

 

                },

                error: function(resp, status, xhr){

               alert('in error json');  

                    alert("Error: " + resp.e); 

                    jQuery("#errorPost").html("Error: " + resp.e  + " - STATUS: " + xhr.status + " " + xhr.statusText);

 

                } 

            });

at server side, java class which is extended for "JiraWebActionSupport" package used for webwork.

@POST

   @Path("/save")

   @Produces("application/json")

   public String SaveIssuesPlanning(String pi)

   {

   logger.info("in saveissuesplanning method");

   logger.info(pi);

   logger.info("end saveissueplanning");

   return "result success found";

   }

Like roshnatprizna likes this
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

is request going to SaveIssuesPlanning method?

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2013

No, does not go to "SaveIssuesPlanning "method so, unable to get log.

I am using - JiraWebActionSupport extend class as it is webwork module.

0 votes
RambanamP
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2013
for your reference
$.ajax({
	        url:  url,
	        data: {term : request.term},
	        dataType: "json",     
	        contentType: "application/json; charset=utf-8",
	        dataFilter: function(data) { return data; },
	        success: function (data) {
			var results = $.map(data, function(item){
			return { value: item.value, id: item.id }}); 
	         cache[request.term] = results; response(results); }
			 });

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2013

Thanks for your comments.

I able to make AJAQ request and posted data get inserted, Now, i want to send result value fail/success to client side at "success" event result parameters (here above example function(data) ). How can we send result from velocity template - .JAVA file (JiraWebActionSupport).. any way to write result, like - response.write(...) in .JAVA file (JiraWebActionSupport).

Thanks

dhaval soni
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2013

Below is the stuff which is used at server side .java file.

here, System.out.println is used to send the result to client side but it does not work. How it could be achivable ?


publicString doIssuesPlanning(){**System.out.println("Success executed result appear");//i want this value to be retrieval at client side but //not working.**}

Suggest an answer

Log in or Sign up to answer