Load url with $.ajax and use token

Asked

Viewed 738 times

-1

O código seguinte não carrega url. O que está errado?



    var token = "XfdfdffddfdfdfferrrrrrrrrrreeeeeeeeeeJ777K";
    var url = "https://producao-talcoisa.com/api/Produto/PesquisarConteudo/";

    $.ajax({

        url : url,
        type : 'GET',
        contentType : "application/json",
        beforeSend: token,
        error : onError,
        success : onSuccess

    });




tokenSession =  null;

var setHeader = function(xhr) {
    xhr.setRequestHeader('tokenApp', token);
    if (token.tokenSession != null) {
        xhr.setRequestHeader('token', token.tokenSession);
    }
};



    function onSuccess() {

        console.log("sucesso");
        // ToastMessage.showMessage("Sucesso");

    }


    function onError() {
        console.log("Erro");
        // ToastMessage.showMessage("Erro");
    }
  • Which error returns in the request?

  • Dude, are you sure the requisition is GET and beforeSend receives token? Try to put POST and send the token as post and see what gives.

  • put date: '&token=' + token, this will look like https://producao-talcoisa.com/api/Produto/PesquisarConteudo/&token=Xfdffddfdfdfferrrrrrrrreeeeej777k

  • I switched to POST but continues.

  • if and to pass the token through the url then concatenate url: url + token,

1 answer

0

Probably your requisition is POST, and it doesn’t make sense for you to pass a string in beforeSend

Try it like this:

$.ajax({

    url : url,
    type : 'POST',
    contentType : "application/json",
    data: {token:token},
    error : onError,
    success : onSuccess

});
  • Still returns error

  • Which error returns?

  • Returns phrase "error" that is in the onerror() method as code placed in the post.

  • See the error in the console, network tab.

  • Believe me, nothing shows up!

  • Enable errors by the server. If it is PHP, add this at the beginning of the file: error_reporting(-1); ini_set('display_errors', 'On');

Show 1 more comment

Browser other questions tagged

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