【问题标题】:send list data from angular to django rest将列表数据从角度发送到 django rest
【发布时间】:2020-10-24 22:42:06
【问题描述】:

我正在学习角度,现在我想将数据数组发送到 django rest 框架,我尝试使用 HttpParams,JSON.stringyfy(),但没有任何工作数据会为空

角度代码:

public getSymptomsList(symptomList:string[]){
    console.log(symptomList)

    return this.httpClient.post(`${this.ApiUrl}diagnos/`, {PASS_DATA_HERE},{headers: new HttpHeaders().set('Authorization', `Token ${this.AuthToken}`),});

  }

Djnago API 代码:

class diagnos(APIView):
    def post(self, request):
        symptomlist = request.POST.getlist('symptoms')
        print(symptomlist)
        ...

但是当我尝试通过httpie 使用终端时,它给出了结果:

bhupesh@Laptop:~$ http -f POST http://127.0.0.1:8000/api/diagnos/ symptoms='fever' symptoms='urinoma'
HTTP/1.1 200 OK
Allow: POST, PUT, OPTIONS
Content-Length: 5037
Content-Type: application/json
Date: Sat, 24 Oct 2020 13:38:57 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.8.5
Vary: Accept, Origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

[
    {
        "name": "uncoordination"
    },
    {
        "name": "fever"
    },
    {
        "name": "pleuritic pain"
    },
    {
        "name": "snuffle"
    },
    {
        "name": "throat sore"
    },
    {
        "name": "malaise"
    },
    {
        "name": "debilitation"
    }
]

【问题讨论】:

    标签: angular api django-rest-framework


    【解决方案1】:

    尝试将您的请求转换为:

    public getSymptomsList(symptoms:string[]){
    
        return this.httpClient.post(`${this.ApiUrl}diagnos/`, {symptoms},{headers: new HttpHeaders().set('Authorization', `Token ${this.AuthToken}`),});
    
      }
    

    使用此代码,主体将是一个具有类似这样的字段符号的对象

    {
    "symptoms": ["a","b","c"]
    }
    

    【讨论】:

    • 请求完成后,您在网络选项卡中看到什么
    • 请求成功,即使我在其中打印了 request.body 打印列表,但它不会作为发布数据进行
    猜你喜欢
    • 2017-09-07
    • 2019-07-10
    • 2021-06-11
    • 2020-09-17
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    相关资源
    最近更新 更多