1

I am facing an issue in sending an ajax get request to download a file served by a REST API in java.

The REST api

 @GET
 @Path("/{modelName}/export")
 @Produces(MediaType.APPLICATION_OCTET_STREAM)

The API is working fine. The problem is with my ajax call

  $.ajax({
                type: "GET",
                url: serverUrl+'/api/models/'+modelName+'/export',
                success : function(res){
                    alert("success");
                    console.log(res);
                },
                error : function(res){
                    alert("error");
                    console.log(res);                
                }
            });

When this call is executed I need the file to be downloaded. How do I modify the ajax call in order to that?

i am getting a success alert when I execute it, and some random strings are printed in the console.

DesirePRG
  • 6,122
  • 15
  • 69
  • 114

1 Answers1

1

You don't need ajax when you download file. Becouse anyway file downloads async by browsers.

You can use plain html for this purpouse:

<a href="YourPath">Download File</a>

But if you want to get file with js you can do it like this:

window.location.href = serverUrl+'/api/models/'+modelName+'/export';
teo van kot
  • 12,350
  • 10
  • 38
  • 70