通过xhr请求令牌时如何解决授权问题?

时间:2018-12-31 17:39:27

标签: javascript json xmlhttprequest authorization

我只想获取令牌以进行进一步的自动授权,这是访问服务器所需的。所以我运行了一个javascript以便获得自动提升。有时要求有效。服务器运行后,服务器错误HTTP500出现。原因还有另一个错误:CORS标头“ Access-Control-Allow-Origin”丢失。出于安全原因,我更改了电子邮件和密码。

如何解决请求?

let email = "abc@ffhs.ch";
let password = "abc_5jjdfhh";
let token = 0;
let credentials = JSON.stringify({"email" : email, "password" : password});
console.log(credentials);
let xhr = new XMLHttpRequest();
let url = "https://tonyspizzafactory.herokuapp.com/api/auth";
xhr.open("POST", url, true);
xhr.onreadystatechange= function() {

    if( xhr.status === 200) {
        let json = JSON.parse(xhr.responseText);
        token = json.token;
        console.log(json.token);
        console.log(json);
        aToken = "Token already exists";
    } else {
        console.log('fail');
        console.log(xhr.readyState);
    }
};
xhr.setRequestHeader("Authorization", "TOKEN");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(credentials);







xhr =  new XMLHttpRequest();
xhr.open("GET", "https://tonyspizzafactory.herokuapp.com/api/pizzas", true);
xhr.onreadystatechange= function() {
    if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
        var json = JSON.parse(xhr.responseText);
        console.log(json);
        pizzas = json;
// ...
}
};
xhr.setRequestHeader('Authorization',  token);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(null);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Pizza - Little Italy</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <link rel="text/javascript" href="auth.js">

    <link rel="stylesheet" href="nav.css">
    <link rel="stylesheet" href="menu.css">
    <link rel="stylesheet" href="structure.css">

    <link rel="text/javascript" href="auth.js">
    <script src="auth.js"></script>



    <script  type="text/javascript" src="auth.js">
        setInterval("my_function();",10000);

        function my_function(){
            alert("ready");
            loadM();
            // place your code here ...
        }

    </script>




</head>


<body onload="contentLoader()">

<div class="container">

    <header>
        <a id="header-a" href="index.html" >Little Italy</a>
    </header>




    <nav id="site-navigation">

        <div class="topnav">
            <label for="toggle-mobile-menu" aria-label="Menu">&#9776;</label>
            <input id="toggle-mobile-menu" type="checkbox" />



            <ul id="mobilnav">
                <li><a href="index.html">Home</a></li>
                <li><a href="pizza.html" class="active">Pizza</a></li>
                <li><a href="insalata.html">Insalata</a></li>
                <li><a href="softdrinks.html">Soft Drinks</a></li>
                <li><a href="feedback.html">Feedback</a></li>

                <a href="javascript:void(0);" class="icon" onclick="myFunction()">
                    <i class="fa fa-bars"></i> </a>
            </ul>

        </div>

    </nav>














    <main>

        <h1>Pizza</h1>

        <div class="welcome" id="menu-pizzas">



            <div class="menu-item">

                <img src="images/piccante.png" alt="Pizza piccante" class="img-item">

            <div class="name-item">
                <label>Pizza piccante</label>
            </div>

            <div class="des-item">
                <small>Tomato, mozzarella, spicy salami, chilies, oregano</small>
            </div>

            <div class="bottom-item">


                <span class="price">12 $</span>
                <button onclick="Outputpiccante()"><img src="images/basket.png" alt="basket" class="basket"></button>


            </div>

        </div>



            <div class="menu-item">

                <img src="images/hawaii.png" alt="Tomato salad" class="img-item">

                <div class="name-item">
                    <label>Pizza hawaii</label>
                </div>

                <div class="des-item">
                    <small>Tomato, mozzarella, pineapples</small>
                </div>

                <div class="bottom-item">


                    <span class="price">12 $</span>
                    <button onclick="Outputhawaii()"><img src="images/basket.png" alt="basket" class="basket"></button>



                </div>

            </div>



            <div class="menu-item">

                <img src="images/funghi.png" alt="Parmesan salad" class="img-item">

                <div class="name-item">
                    <label>Pizza funghi</label>
                </div>

                <div class="des-item">
                    <small>Tomato, mozzarella, fresh mushrooms, oregano</small>
                </div>

                <div class="bottom-item">


                    <span class="price">12 $</span>
                    <button onclick="Outputfunghi()"><img src="images/basket.png" alt="basket"  class="basket"></button>



                </div>

            </div>



            <div class="menu-item">

                <img src="images/giardino.png" alt="Greek salad" class="img-item">

                <div class="name-item">
                    <label>Pizza giardino</label>
                </div>

                <div class="des-item">
                    <small>Tomato, mozzarella, ortichokes, parmesan, garlic</small>
                </div>

                <div class="bottom-item">


                    <span class="price">11 $</span>
                    <button onclick="Outputgiardino()"><img src="images/basket.png" alt="basket"  class="basket"></button>



                </div>

            </div>



            <div class="menu-item">

                <img src="images/quattrostationi.png" class="img-item" alt="Green salad" >

                <div class="name-item">
                    <label>Pizza quattro stationi</label>
                </div>

                <div class="des-item">
                    <small>Tomato, mozzarella, fresh mushrooms, ham, peperoni,
                        pineapples</small>
                </div>

                <div class="bottom-item">


                    <span class="price">13 $</span>
                    <button onclick="Outputquattrostationi()"><img src="images/basket.png" alt="basket"  class="basket"></button>


                </div>

            </div>



            <div class="menu-item">

                <img src="images/rustico.png" alt="Tomato salad" class="img-item">

                <div class="name-item">
                    <label>Pizza rustico</label>
                </div>

                <div class="des-item">
                    <small>Tomato, mozzarella, olive, onion, peperoni, oregano</small>
                </div>

                <div class="bottom-item">


                    <span class="price">11 $</span>
                    <button onclick="Outputrustico()"><img src="images/basket.png" alt="basket"  class="basket"></button>



                </div>

            </div>



            <div class="menu-item">

                <img src="images/stromboli.png" alt="Parmesan salad" class="img-item">

                <div class="name-item">
                    <label>Pizza stromboli</label>
                </div>

                <div class="des-item">
                    <small>Tomato, mozzarella, olive, bacon, oregano, chili</small>
                </div>

                <div class="bottom-item">


                    <span class="price">13 $</span>
                    <button onclick="Outputstromboli()"><img src="images/basket.png" alt="basket"  class="basket"></button>



                </div>

            </div>



            <div class="menu-item">

                <img src="images/alluovo.png" alt="Greek salad" class="img-item">

                <div class="name-item">
                    <label>Pizza all'uovo</label>
                </div>

                <div class="des-item">
                    <small>Tomato, mozzarella, olive, onion, chili, egg, egg,egg</small>
                </div>

                <div class="bottom-item">


                    <span class="price">13 $</span>
                    <button onclick="Outputalluovo()"><img src="images/basket.png" alt="basket" height="30" width="30"></button>


                </div>

            </div>


        </div>






</main>











    <div id="opening">
        <p>Hours:<br>
            <span class="time">Mon-Thu: 11a.m. - 23p.m.<br>
                    Fri-Sat: 11a.m. - 24p.m.<br>
                    Sun: closed </span></p>
    </div>




    <div id="cite">
        <blockquote>"Tasty pizzas which make you feel you get them directly from Italy." John McArthur</blockquote>
    </div>




    <div id="address">
        Address:
        <address>
            Little Italy<br>
            345 Robson St<br>
            Vancouver<br>
            BC V6B 6B3<br>
            Canada

        </address>

    </div>



</div>

</body>

</html>

0 个答案:

没有答案