Badmiral
Badmiral

Reputation: 1589

ajax call link to new page not returning new page

I have this ajax code:

$.ajax({
       type: "POST",
       url: "${createLink(controller:'MyController', action:'downloadFile') } target=\"_blank\" ",
       data: {'names':JSON.stringify(names)},
       dataType: 'json'
});

Which calls this controller method, which works

def downloadFile =
{
    List<JSON> Mynames = JSON.parse(params.names)
    Mynames.each{println "MY ID is: $it"}

    [Mynames:Mynames]
}

It prints out the MY ID Is Lines, but it doesn't render the downloadFile View which is this:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
   <head>
    <title></title>
   </head>
   <body>
    test
   </body>
</html>

However, it is not rendering it. How can I fix this? Thanks.

Upvotes: 0

Views: 130

Answers (1)

Gregg
Gregg

Reputation: 35864

Please take some time to do some reading on Ajax and what it is used for and how it works. You have a fundamental misunderstanding.

$.ajax({
       type: "POST",
       url: "${createLink(controller:'MyController', action:'downloadFile') } target=\"_blank\" ",
       data: {'names':JSON.stringify(names)},
       dataType: 'json'
});

The above code submits a request to your controller. Here's the key point; it does not refresh your page or put any data on your page. It only returns data to you in JavaScript.

You're controller isn't even returning the correct response based on your $.ajax() functions dataType parameter. The dataType property tells the $.ajax() function what kind of data to expect as the response.

jQuery $.ajax() docco

If you need to render HTML as a response to your ajax request, the easiest way is to use the $.load() function.

There are a few other ways but for the sake of keeping things simple for you I won't go into them here. Seriously though, not trying to be rude, but I've read some of your other questions. You really need to get a better understanding of the tools you're using.

Upvotes: 1

Related Questions