How to Post data in web panel in jira(Velocity)

I have used jira sdk to create a plugin include web panel.I can used simple Javascript code to get an value of input box in web panel and use "alert()" method to show the value in front page.But I can't find any way to post data to other where.I've tried AJAX and jQuery but nothing changed.Or is there any way to get input value in Velocity from Java class?

1 answer

This widget could not be displayed.
Alexey Matveev Community Champion Dec 07, 2017

Do you want to POST data from the web panel? instead of alert(), put something like this

AJS.$.ajaxSetup({
    baseUrl: "http://localhost:8090/jira" // The base url of your instance
});

AJS.$.ajax({
    url : '/rest/jiragroupmanagementrestresource/latest/group/FOO",
    type: 'POST',
    data : {}, // form data
    cache : false,
    success: function(response){
        alert('Success');
    }
});

Thank you for your reply.

I tried this code,put them into "

function myFunction() {}

"

Then I use jira quick reload but the web panel throw an error "Error rendering myjava class"

What's wrong?

Alexey Matveev Community Champion Dec 07, 2017

It is difficult to say without seeing your code. What is myjava.class?

I just copy the code you reply to me in last question.

package com.example.plugins.tutorial;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.plugin.webfragment.contextproviders.AbstractJiraContextProvider;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.user.ApplicationUser;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

public class DueDateIndicator extends AbstractJiraContextProvider
{
private static final int MILLIS_IN_DAY = 24 * 60 * 60 * 1000;



@Override
public Map getContextMap(ApplicationUser applicationUser, JiraHelper jiraHelper) {
Map contextMap = new HashMap();
Issue currentIssue = (Issue) jiraHelper.getContextParams().get("issue");
Timestamp dueDate = currentIssue.getDueDate();
if (dueDate != null)
{
int currentTimeInDays = (int) (System.currentTimeMillis() / MILLIS_IN_DAY);
int dueDateTimeInDays = (int) (dueDate.getTime() / MILLIS_IN_DAY);
int daysAwayFromDueDateCalc = dueDateTimeInDays - currentTimeInDays + 1;
contextMap.put("daysAwayFromDueDate", daysAwayFromDueDateCalc);
}
return contextMap;
}
}

I think it's nothing wrong with this java class. 

Alexey Matveev Community Champion Dec 07, 2017

And what is in your vm file? Where did you add the Ajax call?

<input id="dStart" name="dStart" type="text" class="text medium-field" />
<script language="javascript" type="text/javascript">
Calendar.setup({
firstDay : 1,
inputField : 'dStart',
button : 'dStart_trigger',
align : 'Br',
singleClick : true,
showsTime : false,
useISO8601WeekNumbers : false,
ifFormat :'%Y-%m-%d' //'%d/%m/%Y'
});
</script>
<button onclick="myFunction()">Post data</button>
<button onclick="newFunction()">Post</button>
<script language="javascript" type="text/javascript">
function myFunction() {
var x = document.getElementById('dStart').value;
AJS.$.ajaxSetup({
baseUrl: "http://localhost:8090/jira" // The base url of your instance
});
AJS.$.ajax({
url : '/rest/jiragroupmanagementrestresource/latest/group/FOO",
type: 'POST',
data : {}, // form data
cache : false,
success: function(response){
alert('Success');
}
});
alert(x)
}
</script>


Alexey Matveev Community Champion Dec 07, 2017

This script is in the due-date-indicator.vm file? You replaced the old content?

Alexey Matveev Community Champion Dec 07, 2017

Write it like this

<input id="dStart" name="dStart" type="text" class="text medium-field" />
<script language="javascript" type="text/javascript">
Calendar.setup({
firstDay : 1,
inputField : 'dStart',
button : 'dStart_trigger',
align : 'Br',
singleClick : true,
showsTime : false,
useISO8601WeekNumbers : false,
ifFormat :'%Y-%m-%d' //'%d/%m/%Y'
});
</script>
<button onclick="myFunction()">Post data</button>
<button onclick="newFunction()">Post</button>
<script language="javascript" type="text/javascript">
function myFunction() {
var x = document.getElementById('dStart').value;
AJS.ajaxSetup({
baseUrl: "http://localhost:8090/jira" // The base url of your instance
});
AJS.ajax({
url : '/rest/jiragroupmanagementrestresource/latest/group/FOO',
type: 'POST',
data : {}, // form data
cache : false,
success: function(response){
alert('Success');
}
});
alert(x);
}
</script>

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Friday in Jira

New 5k User Limit and Other Cloud Updates

Hi all! Lauren here from the Enterprise Cloud marketing team.  I have some exciting Cloud updates from #AtlassianSummit that I wanted to share. We want everyone in your organization to be ab...

98 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you