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

0 vote
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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

767 views 2 19
Join discussion

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