SyntaxError: Unexpected token < in JSON at position 0 - Javascript 新手

时间:2021-02-14 04:13:32

标签: javascript html json google-chrome

我对 Javascript 很陌生。我正在尝试为电子邮件应用程序提交撰写表单。目的是发送邮件然后加载邮箱。我收到一个错误:

POST http://127.0.0.1:8000/emails 500 (Internal Server Error)

并且无法在代码中的 fetch('/emails', ...) 位置加载。

我也遇到了错误,

SyntaxError: Unexpected token < in JSON at position 0.

这发生在最后,从底部向上 5 行,在:

.catch(error => {
  console.log(error);

我查找了错误,我知道它可能返回 HTML 而不是 JSON。我对此很陌生,因此详细解释事情发生的原因会对我有很大帮助。

inbox.js:

document.addEventListener('DOMContentLoaded', function() {

    // Use buttons to toggle between views
    document.querySelector('#inbox').addEventListener('click', () => load_mailbox('inbox'));
    document.querySelector('#sent').addEventListener('click', () => load_mailbox('sent'));
    document.querySelector('#archived').addEventListener('click', () => load_mailbox('archive'));
    document.querySelector('#compose').addEventListener('click', compose_email);

    // By default, load the inbox
    load_mailbox('inbox');
});

function compose_email() {

    // Show compose view and hide other views
    document.querySelector('#emails-view').style.display = 'none';
    document.querySelector('#compose-view').style.display = 'block';

    // Clear out composition fields
    document.querySelector('#compose-recipients').value = '';
    document.querySelector('#compose-subject').value = '';
    document.querySelector('#compose-body').value = '';

}

function load_mailbox(mailbox) {

    // Show the mailbox and hide other views
    document.querySelector('#emails-view').style.display = 'block';
    document.querySelector('#compose-view').style.display = 'none';

    // Show the mailbox name
    document.querySelector('#emails-view').innerHTML = `. 
       <h3>${mailbox.charAt(0).toUpperCase() + mailbox.slice(1)}</h3>`;


    document.querySelector('#compose-form').onsubmit = () => {

        fetch('/emails', {
            method: 'POST',
            body: JSON.stringify({
                //recipients: document.querySelector('#compose-recipients').value,
                //subject: document.querySelector('#compose-subject').value,
                //body: document.querySelector('#compose-body').value
            })
        })
            .then(response => response.json())
            .then(result => {
                console.log(result);

                if ("message" in result) {
                    load_mailbox('sent');
                }

                if ("error" in result) {
                    // There was an error in sending the email
                    // Display the error next to the "To:"
                    document.querySelector('#to-text-error-message').innerHTML = result['error']

                }
                console.log(result);
                console.log("message" in result);
                console.log("error" in result);
            })
            .catch(error => {
                console.log(error);
            });
        return false;
    }
};

0 个答案:

没有答案