Debopam Parua
Debopam Parua

Reputation: 530

Sending custom headers required to load a page

I have a webpage that requires an id and a token to be sent in the header for the page to load. A php login script returns the id and token. Then this id and token needs to be sent in the header of the page for it to load. Check is provided to look for these custom header, if not set, it is redirected to the login page. The page needs to be loaded from a javascript function. If I use window.location, it is redirected to the login page as the headers are not found. How do I achieve this?

Here is my code:

$.ajax({
    type: "POST",
    url: "http://www.example.com/apis/login.php",
    data: JSON.stringify(inputs),
    dataType: "json",
    success: function(data) {
        if(data.status == "success") {
            username = data.username;
            uid = data.uid;
            token = decodeURIComponent(data.token);
            loadpanel(username, uid, token);
        }
        else{
            alert(data.message);
        }
    },
    error: function() {
        alert("could not connect to server");
    }
});

function loadpanel(username, uid, token) {
    $.ajax({
        async: true,
        type: "GET",
        url: "/haspanel.php",
        headers: {
            "U-Name" : username,
            "U-Id" : uid,
            "Auth-Token" : encodeURIComponent(token)
        },
        success: function(data) {
            window.location='/haspanel.php';
        },
        error: function() {
            alert("could not connect to server");
        }
    });
}

I know, this won't work, but I don't understand what is the workaround.

Upvotes: 0

Views: 1425

Answers (1)

Ashil John
Ashil John

Reputation: 7742

You could just do this right? :

window.location = '/page.php?id='+ id +'&token='+ token +'';

Where id and token are variables that contain the required values of your id and variable parameters.

Upvotes: 1

Related Questions