我有一个服务器项目.这是一个html文件.如果我直接从磁盘打开文件,内置的客户端表单验证可以正常工作.但是如果我通过http传输文件,客户端的表单验证就不起作用了.以下是html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <h2>Login</h2>
    <form id="loginForm">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required maxlength="10" pattern="^\S+$">
        <br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required maxlength="10" pattern="^\S+$">
        <br>
        <button type="submit" onclick="submitLoginForm()">Login</button>
        <button type="button" onclick="location.href='register.html'">Register</button>
        <br>
        <div id="error-message"></div>
    </form>

    <script>
        async function submitLoginForm()
        {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            try
            {
                const response = await fetch('/api/login',
                    {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json', },
                        body: JSON.stringify({ username, password }),
                    });

                if (response.status === 200)
                {
                    window.location.href = '/dashboard';
                }
                else if (response.status === 401)
                {
                    const errorMessage = document.getElementById('error-message');
                    errorMessage.textContent = 'Error: Invalid username or password';
                }
                else
                {
                    const errorMessage = document.getElementById('error-message');
                    errorMessage.textContent = 'An error occurred';
                }

            }
            catch (error)
            {
                console.error('Error during login:', error);
            }
        }
    </script>
</body>
</html>

Http响应的头部如下:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 2393
Cache-Control: no-cache, no-store, must-revalidate
Date: Mon, 18 Dec 2023 00:48:59 GMT

我已经在浏览器中判断了开发工具,以下是控制台中的错误和警告:

Error with Permissions-Policy header: Origin trial controlled feature not enabled: 'attribution-reporting'.
Error with Permissions-Policy header: Origin trial controlled feature not enabled: 'run-ad-auction'.
Error with Permissions-Policy header: Origin trial controlled feature not enabled: 'join-ad-interest-group'.
Error with Permissions-Policy header: Origin trial controlled feature not enabled: 'browsing-topics'.
An element doesn't have an autocomplete attribute
A form field has an id or name attribute that the browser's autofill recognizes. However, it doesn't have an autocomplete attribute assigned. This might prevent the browser from correctly autofilling the form.
To fix this issue, provide an autocomplete attribute.
1 resource
Violating node
Learn more: HTML attribute: autocomplete

谢谢你的帮助.:)

推荐答案

我认为,当你直接从磁盘打开HTML文件时,内置的客户端表单验证工作,这表明HTTP响应头或HTML文件的提供方式可能有问题.

以下是一些需要判断的事项:

  • 内容类型标题 将此设置正确地重新判断为text/html; charset=utf-8.

  • 然后,将HTML作为UTF-8: HTML文件以UTF-8编码保存.

  • 然后,判断是否有其他标头:

  • 浏览器缓存: 你不妨清空你的浏览器缓存.

  • HTTPS注意事项: 一些现代浏览器执行更严格的安全策略,所以你最好通过HTTPS为你的应用程序提供服务.

  • 自动完成属性 如果您选中所有这些,最后,最好将autocomplete="off"添加到您的表单域,以防止浏览器自动填充问题.

<input type="text" id="username" name="username" required maxlength="10" pattern="^\S+$" autocomplete="off">
<input type="password" id="password" name="password" required maxlength="10" pattern="^\S+$" autocomplete="off">

希望对您的理解有所帮助.

Html相关问答推荐

springBoot + Thymeleaf:属性中的UTF-8

应用于文本而不是弹性容器的Flexbox属性

带有MathJax SVG的HTML代码在XHTML中不起作用

仅在加载视频时显示描述(<;Video>;标签)

如何使用html和css关键帧创建动画

在一行中对齐文本和图标

在<;style&>中列出的三种字体中,只有两种显示

使用响应迅速的网格系统,将两列保持在同一行,同时允许更多列用于更大的屏幕

如何翻转卡片图像的背面

我无法动态嵌入Instagram帖子,因为无法分配Instagram固定链接

使具有FLEX父项的溢出自动的子元素收缩

希望平稳过渡到悬停状态

在Firefox中使用keySplines时,SVG: <animateMotion>不起作用

模拟另一个输入值设置表单输入的数值

更改标签标签的大小

为什么在使用src与srcdoc时 iframe 内容高度呈现不同?

将 div 放在图片旁边而不是下方

右对齐 bootstrap 导航项而不是下拉菜单

圆形边框显示在该部分后面.怎么修?

无法从社交栏中 Select 选项