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"?
Community moderators have prevented the ability to post new answers.
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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is the error you're getting ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"); } }); } );
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's an array so to get the last element use:
data.fields.attachment[data.fields.attachment.length-1].self
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Where can you get the current issue id from ? What's the context of this JS code ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm actually missing where do you execute that code. For example where does this "issue" objects comes from ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
JIRA issue id. I managed to grab it using a jQuery selector. Thanks though!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.