Run Ajax by passing credential information in Header

Asked

Viewed 2,640 times

2

I need to make an ajax call by passing in Header the following information: Content-Type:application/json Cookie:authToken=valueXPTO (This information above I got from Postman) Exemplo no postam funcionando

I need to do the same thing only I saw Ajax, and there’s no way I’m doing it. Someone can help me?

Follow the code below:

            function LogoutToken(){
        debugger;
            var urlApi = urlBase + "logout";

            $.ajax({
                type: "POST",
                dataType: 'json',
                url: urlApi,
                crossDomain: true,
                async: false,
                contentType: "application/json; charset=utf-8",
                beforeSend: function(xhr) {
                    xhr.setRequestHeader('Cookie', 'authToken=' + Token);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log(textStatus);
                    alert('Falha na consulta de valores específicos, erro:' + textStatus);
                },
                success: function (msg) {
                    var xyx = msg;
                }
            });
        }

When I run this way the following error: Internal Server Error

In C# I execute the following way and in the right way:

        RestRequest request = new RestRequest(Method.POST);
        request.AddCookie("authToken", token);
  • Which error does it present? As a POST I think the 'date' was missing (content of the request).

  • @Andersonsouza he from Internal Server Error. But I have no data to pass. This API is only for killing the token session. For it I need to pass only the session token as example of the Postman image.

  • In Postman it generates the Header like this: Content-Type:application/json Cookie:authToken=webXPTO123

  • which value of urlApi?

  • https://chp.tbe.taleo.net/chp04/ats/api/v1/logout

  • Post a photo of the Headers(16) tab of Postman

  • Or better still in Postman has an option called "Code" selects the Javascript option -> Jquery AJAX, then compares with your request.

  • Thanks, I didn’t really know that part of the Postman code, but even picking up there isn’t going. When I am debugging, it arrives at the cookie line and gives the following error: Refused to set unsafe header "cookie": xhr.setRequestHeader("cookie", "authToken=" + Token);

  • @Andersonsouza had another hidden error, this one:Failed to load https://chp.tbe.taleo.net/chp04/ats/api/v1/logout: Response to preflight doesn’t request pass access control check: The value of the 'Access-Control-Allow-Origin' header in the Response must not be the wildcard '*' when the request’s credentials mode is 'include'. Origin 'null' is therefore not allowed access. The credentials mode of requests initiated by the Xmlhttprequest is Controlled by the withCredentials attribute.

  • The header you requested: access-control-allow-headers x-requested-with, Content-Type, origin, Authorization, Accept, client-security-token access-control-allow-methods POST, GET, OPTIONS, DELETE, PUT access-control-allow-origin * access-control-max-age 1000 cache-control no-cache, no-store, max-age=0 Connection Keep-Alive content-encoding gzip content-language en-BR content-type application/json;charset=UTF-8 date Fri, 17 Nov 2017 16:17:35 GMT expires Thu, 01 Jan 1970 00:00:00 GMT .. Continues

  • Keep-Alive timeout=5, max=100 pragma no-cache server Apache transfer-encoding chunked Vary Accept-Encoding,

  • Man erases the comment with the security token

  • A relaxes, this token is dev and it changes every session. Every time I order here I delete by Postman.

  • Blz.. this error: :Failed to load chp.tbe.Taleo.net/chp04/Ats/api/v1/logout , is Cors but this is from the served side. a search how to enable Cors ai in api

  • Take a look at this one: https://stackoverflow.com/questions/33143776/ajax-request-refused-to-set-unsafe-header I think you’ll answer

Show 10 more comments

1 answer

0

Try to put the Header this way inside the ajax configuration object

"headers":{
    "Cookie":"authToken=valorXPTO
}

Source: Jquery documentation

  • Nothing... Dude, I’ve been trying to make this work since yesterday.. Ta Difícil...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.