Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to grab Attachment ID on Issue Page using JS?

Anthony Madrigal March 31, 2014

I am using an ajax call to whose url needs the attachment id. I am using a hardcoded url right now which is url: AJS.contextPath()+"/rest/api/latest/attachment/10415"

jQuery.ajax({
		url: AJS.contextPath()+"/rest/api/latest/attachment/10415",
		TYPE: "GET",
		//dataType: "application/json",
		dataType: "text",
		success: function(data){
			console.log("Success");
		},
		error: function(data){
			console.error("Error with attachments");
		},
	}).done(function(data){
		
	console.log("Done with greying attachments");
	});

So this only works for the attachment with id 10415. I was thinking of making 2 ajax calls, the above one being the second. And the first having a url of AJS.contextPath()+"/rest/api/latest/issue/${issueId} which returns the content url of the above ajax call.

I am new to ajax so I am not sure if this will work. Also, should the dataType be "text" or "application/json"?

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Boris Georgiev _Appfire_
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.
March 31, 2014

The first should have dataType "application/json" Tand from the returned json object you can get the attachment id, so you can later pass it to the second ajax call. You have to make the second call in the success method of the first and to get the attachment url you can call var url = data.attachment[0].self.

Something like:

jQuery.ajax({
    url: AJS.contextPath()+"/rest/api/2/issue/MKT-2",
    TYPE: "GET",
    dataType: "application/json",    
    success: function(data){
			jQuery.ajax({
				url: data.attachment[0].self,
				TYPE: "GET",				
				dataType: "text",
				success: function(data){
					console.log("Success");
				},
				error: function(data){
					console.error("Error with attachments");
				}
			});
    }    
});

Just try it and tell if it works.

Anthony Madrigal March 31, 2014

i first tried with just the first ajax call with the success just being a console log but it returned an error. Am i missing something?

Boris Georgiev _Appfire_
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.
March 31, 2014

What is the error you're getting ?

Anthony Madrigal March 31, 2014

I made dataType to "json" instead of "application/json" and it makes the first ajax successful. However when trying to call the second ajax call, it says that Uncaught TypeError: Cannot read property '0' of undefined

Boris Georgiev _Appfire_
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.
March 31, 2014

Change the code to :

AJS.$.getJSON(AJS.contextPath()+"/rest/api/2/issue/MKT-2", function(data){
         jQuery.ajax({
                url: data.fields.attachment[0].self,
                TYPE: "GET",                
                dataType: "text",
                success: function(data){
                    console.log("Success");
                },
                error: function(data){
                    console.error("Error with attachments");
                }
            });
    }    
);

Anthony Madrigal March 31, 2014

instead of using 0, how would i grab the latest attachment?

Anthony Madrigal March 31, 2014

great! that worked. thanks

0 votes
Boris Georgiev _Appfire_
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.
March 31, 2014

It's an array so to get the last element use:

data.fields.attachment[data.fields.attachment.length-1].self


Anthony Madrigal March 31, 2014

I want to try these ajax calls with different issues. How do I use the current Issue Id for the url on the first ajax call? Thanks again for all your help!

Boris Georgiev _Appfire_
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.
March 31, 2014

Where can you get the current issue id from ? What's the context of this JS code ?

Anthony Madrigal April 1, 2014

I am not really sure what you mean but I have tried doing url: AJS.contextPath()+"/rest/api/2/issue/"+${issue.id}

along with different variations for the issue id

Boris Georgiev _Appfire_
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 1, 2014

I'm actually missing where do you execute that code. For example where does this "issue" objects comes from ?

Anthony Madrigal April 1, 2014

JIRA issue id. I managed to grab it using a jQuery selector. Thanks though!

TAGS
AUG Leaders

Atlassian Community Events