【问题标题】:fetch is returning html rather than JSON responsefetch 正在返回 html 而不是 JSON 响应
【发布时间】:2021-10-19 13:18:24
【问题描述】:

我正在开发 cs50 网络开发项目 Network。基本上是在构建一个 twitter 模仿者。

它仍在处理中,但到目前为止,我有两个获取 JSON 响应的请求。一次完美运行,但另一个非常相似的请求返回 html 而不是 JSON,从而导致错误。无法弄清楚为什么这个不起作用。代码如下:sn-ps:

这是出于某种原因为我的 profile.html 文件返回 html 的文件。注释掉的部分是实际的 fetch JSON 请求,但我暂时将其更改为在控制台中显示返回的内容。 profile.js:

function load_posts_profile() {

    console.log("load_posts_profile running");

    document.querySelector('#prof-posts').style.display = 'block';

    fetch(`/profile_posts`)
    //.then(response => response.json())
    .then(response => response.text())
    .then(text => console.log(text))
    //.then(posts => {
        // Print posts
        //console.log(posts);
        //posts.forEach(post => show_posts_profile(post));
    //});

}

profile.html:

{% extends "network/layout.html" %}
{% load static %}

{% block body %}
    {% if user.is_authenticated %}
        <!--insert profle name below in h3-->
        <h2 id="profile-name"></h2>
        <br>
        <h4 id="followers"></h4>
        <br>
        <h4 id="following"></h4>

        <!--add js to load user's posts only-->
        <div id="prof-posts">
        </div>

    {% else %}
        <strong> Login To See Profile</strong>
    {% endif %}

{% endblock %}

{% block script %}
    <script src="{% static 'network/profile.js' %}"></script>
{% endblock %}

urls.py:

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),
    path("all_posts", views.all_posts, name="all_posts"),
    path("<str:poster>", views.profile, name="profile"),
    path("profile_posts", views.profile_posts, name="profile_posts")
]

views.py sn-p:

def all_posts(request):

    posts = Post.objects.all()
    posts = posts.order_by("-timestamp").all()

    return JsonResponse([post.serialize() for post in posts], safe=False)

def profile_posts(request, poster):

    posts = Post.objects.get(poster=poster)
    posts = posts.order_by("-timestamp").all()

    return JsonResponse([post.serialize() for post in posts], safe=False)


def profile(request, poster):

    return render(request, "network/profile.html", {
        "poster": poster,
    })

这是具有类似获取请求的 js 文件,它可以完美运行。为什么这个工作(即返回 JSON)而另一个返回 html?:

function load_posts() {

    console.log("load_posts running");

    document.querySelector('#all-posts').style.display = 'block';



    fetch(`/all_posts`)
    .then(response => response.json())
    .then(posts => {
        // Print posts
        console.log(posts);
        posts.forEach(post => show_posts(post));
    });

}

【问题讨论】:

    标签: html django api fetch


    【解决方案1】:

    当在调试模式下,django 在发生错误时返回一个 html 响应,通常这就是问题所在,我认为您正在将参数海报传递给视图函数,但您在 url 路径中没有例外

    以后有几件事要记住,当您收到与预期不同的响应时,您应该检查终端中的日志或检查控制台客户端中的响应代码,如果它以 5 开头ot 是服务器错误,ot 以 4 开头,未找到或类似,您应该检查日志

    以下是修改代码的方法: 将 str:poster 添加到 url 路径并修改 fetch 请求以将其包含在您的 url 中

    【讨论】:

    • 感谢您的回答。您介意对我的代码进行更具体的修改吗?我不确定我明白在哪里/添加什么。
    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多