HTML JavaScript (none same origin/external URL) fetch POST to Heroku Django return 500 (Internal Server Error)

问题描述

我确实知道有很多人在问非常相似的问题。但我试着环顾四周,似乎仍然没有得到适合我的项目的答案。

正如所提到的标题,我试图通过 fetch POST 通过外部网站访问 Heroku 上的 Django,而不是同一个来源。目的是让网站向我的 Django 项目发送一组数据,并处理数据,并使用处理后的数据进一步响应获取

这是我的 Django:

@csrf_exempt
# Create your views here.
def fetchPost(request):
    #python post request
    if request.method == 'POST':
        data=json.loads(request.body)
        pythonDict={"hello":"hey,I am Kor POST! "}
        x=pythonDict.hello+data
        y=json.dumps(x)
        #result of a JSON string
        return HttpResponse(y)

我在设置中同时使用了 csrf_exemptCORS_ALLOW_ALL_ORIGINS = True 来使 JavaScript 与 Django 之间的通信顺畅

看看我的 JavaScript:

fetch('https://some-example-project.herokuapp.com/fetch_POST/',{
                headers:{"Content-Type":"application/json","Accept": "application/json"},method:"POST"
            },{
                body:JSON.stringify("Some random content")
            })
            .then((res) => {
                return res.text();
                console.log(res);
                document.getElementById("Fetch").innerHTML= res;
            })

期待是否有人可以根据我的情况提供解决方案。绝对非常感谢

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)