Search code examples
jqueryajaxhref

Set href value in ajax post request


I have java servlet application with jquery, in my application I have datatable with movie,

var ProjectionManager = {
getAll : function(){
            $.get('ProjectionsServlet', function(data){
                $('#projectionsTable1').DataTable ({
                    data: data.projections,
                    paging: false,
                    info: false,
                    searching: false,
                    autoWidth: true,
                    columns: [
                            {
                            "data": 'movie',
                            render:function(data, type, row){
                                return '<a id="movieRedirectFromProjection" data-name="' + data + '" href="#">' + data + '</a>';
                                }
                            },
                            {data : 'dateOutput'}
                        ]
                })
            })
        }
}

When I click on movie column, I want to update href value in my browser with id of that movie. I don't have movie id value in this table, so I made query to database where it returns whole movie object by title.

var MoviesManager = {
getMovieByTitle: function(title) {
            params = {
                'action': 'getMovieByTitle',
                'title': title
            };
            $.post('MoviesServlet', params, function(data){
                //I want to set href value here with data.movie.id that comes from servlet
                //I think it's something like this         e.target.href = "#";
                //                                         e.target.href += '?id=' + data.movie.id;
                //but I can't use event here, can I?
            })
        }
}

This is how I handle click on movie column in datatable

$('body').on('click', '#movieRedirectFromProjection', function(e){
        var name= $(this).attr("data-name");
        MoviesManager.getMovieByTitle(name);

    });

Solution

  • Use window.location.href to get the url of the browser and assign the value to the same after appending the id