首页 > 解决方案 > 获取 api javascript 返回 html 代码

问题描述

现在我不知道为什么实际上会发生这种情况,而之前 fetch 的功能是在我定位的 url 上正确显示承诺响应。但现在我在console日志中看到的只是登录页面的 html 代码,我没有更改任何内容,但为什么会发生这种情况?

const caseform = _doc.forms.namedItem('caseform');

    caseform.addEventListener("submit", e => {
        e.preventDefault();

        this._action = caseform["action"];
        this._method = caseform["method"];

        this._fd = new FormData(caseform);

        async function savecase() {
            this._params = {method: this._method, body: this._fd};
            this._response = await fetch(this._action, this._params);

            try {
                if(this._response.ok) {
                    return await this._response.text();
                }
            } catch(error) {
                return error;
            }
        }

        savecase()
            .then(result => {
                if(result === 'error_in_retention_requirements') {
                    _doc.querySelector('div[data-result-case-error]').style.display = 'block';
                    _doc.querySelector('div[data-result-case-error]').innerHTML = 'Please provide all rentention requirements.';
                    _doc.querySelector('div[data-result-case-error]').scrollIntoView();
                } else if(result === 'error_in_clearance_requirements') {
                    _doc.querySelector('div[data-result-case-error]').style.display = 'block';
                    _doc.querySelector('div[data-result-case-error]').innerHTML = 'Please provide all clearance requirements.';
                    _doc.querySelector('div[data-result-case-error]').scrollIntoView();
                }  else if(result === 'error_in_clearance_requirements') {
                    _doc.querySelector('div[data-result-case-error]').style.display = 'block';
                    _doc.querySelector('div[data-result-case-error]').innerHTML = 'Please provide all clearance requirements.';
                    _doc.querySelector('div[data-result-case-error]').scrollIntoView();
                } else {
                    console.log(result);
                }
            });
    });

这是在控制台日志中返回的返回文本

新案例:136

<!DOCTYPE html>
<html class="no-js" lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>v1.0</title>
    <link rel="icon" href="http://localhost/itd_project/darsystem_v1.0/public/files/img/logo.jpg" type="image/gif" sizes="16x16">
    <link rel="stylesheet" href="http://localhost/itd_project/darsystem_v1.0/public/files/foundation/css/foundation.css">
    <link rel="stylesheet" href="http://localhost/itd_project/darsystem_v1.0/public/files/foundation/css/app.css">
  </head>
  <body>

<script>
const _w = window;
const _doc = document;

_w.onload = () => {
    const pcode = _doc.querySelector('input[type="password"]');

    pcode.onpaste = (e) => {
        e.preventDefault();
    }
}</script>


    <div class="row-login">
        <form action="http://localhost/itd_project/darsystem_v1.0/login/check/?attempt=1&amp;for=dXNlcl9sb2dpbg==" method="post" class="stacked" style="color: #272822; ">
            <div class="row stacked">
                <img class="logo-login" src="http://localhost/itd_project/darsystem_v1.0/public/files/img/logo.jpg"/>
                <h5 class="text-center" style="color: #000000; font-size: 12px;">Department of Agrarian Reform</h5>
            </div>
            <label>ID
                <input type="text" name="username" placeholder="ID Number" />
            </label>
            <label>Password 
                <input type="password" name="password" placeholder="&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;&#9679;" autocomplete="off" />
            </label>
            <p>
                <button type="submit" class="dar_login button expanded" style=" color: #fff; box-shadow: 0 2px 4px rgba(10, 10, 10, 0.4);">Log in</button>
            </p>
        </form>
    </div>

<footer style="margin-top: 120px;">
      <p class="footer">&copy; 2018</p>
</footer>
  </body>
</html>

标签: javascriptfetch-api

解决方案


推荐阅读