4

I need to create in either Javascript or jQuery an http request. In it I need to put some header fields and then send this all to an external url that is on the same server. The target web page needs to read these headers. In short, I need to make a sort of redirect, but which contains header parameters provided. I am trying to do it in the flowing way but it does not redirect:

    var mdata = {roles: roles};
    $.ajax({
        url: "SendRequest",
        type: "POST",
        data: mdata,
        //crossDomain: true,
        dataType: "json"

    })

            .done(function (data) {

                 $.ajax({
                 url: "/OimPortalEmulation",
                 type: "POST",

                 beforeSend: function(xhr){xhr.setRequestHeader('name', 'Salvo'); xhr.setRequestHeader('groups', data)}
                 }).done(function(){})
                 .fail(function(){alert('Redirect Failed!')});



            })
            .fail(function (data) {
                alert("ajax error! ");
            })
            .always(function () {

            });

This works in as much as it calls the target servlet which prints out successfully the header provided, but it does no open a new tab nor does it show the pag in the current tab. How can I resolve this?

FYI: I am using Java Servlets on the server side.

Edit: I provided further details on the problem posted.

Salvo
  • 541
  • 6
  • 19
  • After successfully POSTing to the servlet you want javascript to do what exactly? Inside that `.done(function(){})` is where you need to put some logic. – Matt Mar 28 '17 at 19:22
  • The target web app has its own home page. When the ajax call completes I want it to open that home page, but it doesn't it just stays in the current page. In short it needs to be something like a redirect, but without losing the header parameters. The way I shown here is not doing. I put it here just to show what I managed to get so far. I would like it if you could show me a "from scratch" solution – Salvo Mar 28 '17 at 19:42
  • I'm having difficulty understanding what you're trying to do but it seems like you want to redirect to another web app. In that case, I don't think you can really set the headers. Instead of ajax you would POST data and read it on the other side. Redirecting upon success from the ajax will lose the headers for sure. The headers are only valid within the context of that request. – Matt Mar 28 '17 at 19:53
  • I understand. The problem is I need to manipulate the header before giving it to the target web app. In general, how could I do that? Lets say I have a form with check box fields and I need to put in a single string all the checked values. Then this one field needs to go into the header and finally the request may go. How could I do that? – Salvo Mar 28 '17 at 20:02
  • Does it need to go in the literal header or can it be posted data and the target web app can read what is posted to it? – Matt Mar 28 '17 at 20:05
  • It needs to be in the header. In java I use the response.addHeader("name", "Salvo"). In ajaxk I posted above instead how I made the headers. Sadly, it needs to be this way... – Salvo Mar 28 '17 at 20:08
  • 1
    I don't think it can be done. Hopefully someone else will chime in with ideas. It's essentially the same problem as this guy has: http://stackoverflow.com/questions/24130004/adding-http-headers-to-window-location-href-in-angular-app#26373493 – Matt Mar 28 '17 at 20:10
  • I understand. How about thrugh java servlets, could it be done through them? I know how to create the header in java servlet, maybe with getRequestDispatcher(url) ? – Salvo Mar 28 '17 at 20:24
  • Hi @Salvo, how did you eventually solve your problem? – Gargaroz Mar 06 '19 at 08:37

0 Answers0