简体   繁体   English

如何外部化我的注销功能?

[英]How can I externalize my logout function?

I'm building a login portal, and the container the portal is in will be cleared and refilled with ajax. 我正在构建一个登录门户,该门户所在的容器将被清除并重新填充ajax。 My test page looks like this: 我的测试页面如下所示:

<input type="button" id="logoutButton" onClick="Logout()" value="Logout"/>
<form id="loginForm">
    Username: <input type="text" id="username"><br>
    Password: <input type="password" id="password"><br>
    <button type="submit">Login</button>
    Login status: <div id="loginStatus">No login submitted</div>
    Logout status: <div id="logoutStatus">No logout atttempted</div>
    <br><br>
    <button type="submit">Login</button>
</form>
<script src="javascripts/logout.js"></script>

This is logout.js: 这是logout.js:

    function Logout() 
        {
        var logoutRequest = $.ajax(
            {
            url: "/api/LogOut",
            method: "POST",
            dataType: "json"
            }
        }

        logoutRequest.done(function( response ) 
        {
            $('#logoutStatus').text('Error: ' + response.error + ', Message: ' + response.message);
        });

        logoutRequest.fail(function( jqXHR, textStatus ) 
        {
            alert( "Request failed: " + textStatus );
        });
    });

I had this working when it was previously internal (alongside the login script), but I'm not terribly clear on the syntax when it comes to calling it externally. 当它以前是内部的(与登录脚本一起使用)时,我已经可以使用它了,但是在外部调用它时,我在语法上并不十分清楚。 I've spent the morning looking over various examples on the web, but most examples I find are only related to basic strings. 我花了整个上午在网上查看各种示例,但是我发现的大多数示例仅与基本字符串有关。

How can I make Logout() work outside of the main html page? 如何使Logout()在html主页之外工作?

The script tag in the html is valid. html中的script标签有效。 It looks like your brackets are wrong. 看来您的括号是错误的。 Try this. 尝试这个。

function Logout() {
    var logoutRequest = $.ajax({
        url: "/api/LogOut",
        method: "POST",
        dataType: "json"
    });

    logoutRequest.done(function( response ) {
        $('#logoutStatus').text('Error: ' + response.error + ', Message: ' + response.message);
    });

    logoutRequest.fail(function( jqXHR, textStatus ) {
        alert( "Request failed: " + textStatus );
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM