Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: XHR Not

  1. #1
    Regular Coder
    Join Date
    Jan 2016
    Posts
    274
    Thanks
    53
    Thanked 3 Times in 3 Posts

    XHR Not

    Hello,


    I am trying to use PouchBD and get my document from the indexdb and pass it as my doc.access_token to the Authorization Bearer, my problem is that the xhr.setRequestHeader('Authorization', 'bearer ' + doc.access_token); is not being triggered. It works outside db.get('document').then(function (doc) { but not inside, it is missing in my request header. Any idea why please?



    Code:
               $.ajax({
                    url: serverUrl + '/api/login',
                    method: 'POST',
                    dataType: "json",
    
                    beforeSend: function (xhr) {
                      db.get('document').then(function (doc) {   
                      xhr.setRequestHeader('Authorization', 'bearer ' + doc.access_token);
                      app.dialog.progress('Checking Access Token<br>please be patient...');
                    }).catch(function (err) {
                      console.log(err);
                     });
                    },
                    statusCode: {
                      //If status is 200
    
                      200: function (response) {
                        console.log("Update Database");
                        // db.transaction(function (tx) {
                        //   tx.executeSql('UPDATE INTO oauth2 (id,client_id, client_secret, access_token, refresh_token, username, password) VALUES (?,?,?,?,?,?,?) WHERE id = ?', [1, clientId, clientSecret, response.access_token, response.refresh_token, usernameInput, passwordInput, 1]);
    
                        // }, function (error) {
                        //   console.log('transaction error: ' + error.message);
                        // }, function () {
                        //   console.log('transaction ok');
                        //   app.dialog.close();
                        //   app.router.navigate('/createreport/');
                        // });
    
                      },
    
                      401: function (response) { //Unauthenticated
                        app.dialog.close();
                        console.log("error 401 Unauthenticated");
                        app.router.navigate('/login/');
    
                      },
                      500: function (response) { //error
                        app.dialog.close();
                        console.log("error 500");
                        app.router.navigate('/login/');
                      },
                      404: function (response) { //Not found
                        app.dialog.close();
                        console.log("error 404 Not found");
                        app.router.navigate('/login/');
                      },
                    }
                  });
    Thanks

  2. #2
    New to the CF scene
    Join Date
    Mar 2018
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    test this please.
    Code:
    <html>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
    <!DOCTYPE HTML>
        <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <script>
            
    
    $.ajax({
                    url:'http://sebastian1012.bplaced.net/Forum_Scripte/json-test.html',
                    method: 'POST',
                    dataType: "json",
    
                    beforeSend: function (xhr) {
                      db.get('document').then(function (){  
                      xhr.setRequestHeader('Authorization', 'bearer ' + doc.access_token);
                      app.dialog.progress('Checking Access Token<br>please be patient...');
                    }).catch(function (err) {
                      console.log(err);
                     });
                    },
                    statusCode: {
                      //If status is 200
    
                      200: function (response) {
                        console.log("Updatebase");
                          db.transaction(function (tx) {
                            tx.executeSql('UPDATE INTO oauth2 (id,client_id, client_secret, access_token, refresh_token, username, password) VALUES (?,?,?,?,?,?,?) WHERE id = ?', [1, clientId, clientSecret, response.access_token, response.refresh_token, usernameInput, passwordInput, 1]);
    
                         }, function (error) {
                          console.log('transactionor: ' + error.message);
                          }, function () {
                           console.log('transaction');
                           app.dialog.close();
                          app.router.navigate('/createreport/');
                         });
    
                      },
    
                      401: function (response) { //Unauthenticated
                        app.dialog.close();
                        console.log("errorUnauthenticated");
                        app.router.navigate('/login/');
    
                      },
                      500: function (response) { //error
                        app.dialog.close();
                        console.log("error");
                        app.router.navigate('/login/');
                      },
                      404: function (response) { //Not found
                        app.dialog.close();
                        console.log("errorNot found");
                        app.router.navigate('/login/');
                      },
                    }
                  });
            
        </script>
        <body>
            
            
        </body>
    </html>

    you have forgotten " and (){

    Check this please in give me answer.
    Sorry for my englisch.
    I come rom germany


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •