我通过nginx设置了一个带Django的API,我试图用Node应用程序命中一个端点,但我收到的响应基本上是index.html文件而不是JSON。
这是我的.conf in sites-available(fake_watson是api,聊天是节点应用程序):
server {
listen 80;
server_name 10.39.40.107;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/fake_watson_frontend/build;
}
location /fake_watson/ {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/fake_watson_backend/fake_watson_backend.sock;
charset_types application/json;
default_type application/json;
add_header content_type application/json;
proxy_set_header content_type application/json;
}
location /chat/ {
proxy_pass http://localhost:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
我在Django中的代码看起来像这样(概率是一个字典):
import json
from .models import Input, Classification
from .classifier import Classifier
from django.http import JsonResponse
# Create your views here.
def predict(request, input):
probabilities = Classifier.predict(input)
return JsonResponse(probabilities)
通过我的axios调用,response.data如下所示:"<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="shortcut icon" href="/favicon.ico"><title>React App</title><link href="/static/css/main.9a0fe4f1.css" rel="stylesheet"></head><body><div id="root"></div><script type="text/javascript" src="/static/js/main.bff3100f.js"></script></body></html>"
最后,application / json包含在我的mimetypes中。如果有帮助,我的内容类型标题仍然是“text / html; charset = UTF-8”。
我一直在寻找一个直截了当的答案,但我似乎没有找到任何东西 - 非常感谢任何帮助!