问题描述
我的URI和查询字符串具有以下格式,今天早上您可以看到它正在工作。
[api-deployment-dev-6b4f758c54-8rwxt api] [10/Aug/2020 10:44:22] "GET /survey/129/results/advanced_query?startingPage=12&dataView=By%20Position HTTP/1.1" 200 2
由于某些原因,尽管它们是我今天更改的仅有的两个文件,但它仍然无法工作,但我无法使其再次工作。我现在不断得到以下信息:
[api-deployment-dev-7cf59c8b8-b6p4k api] Bad Request: /api/survey/129/results/advanced_query/
[api-deployment-dev-7cf59c8b8-b6p4k api] [10/Aug/2020 12:38:54] "GET /survey/129/results/advanced_query/?startingPage=12&dataView=By%20Position HTTP/1.1" 400 0
似乎是react
或axios
,或者其他原因在查询字符串之前添加了/
。为什么,我不确定,因为今天早上没有这样做。
如何防止这种情况发生?
import React,{ useState } from "react";
import {
Button,InputGroup,InputGroupText,InputGroupAddon,Input,} from "reactstrap";
import axios from "axios";
const AdvancedQuerySurveyDataView = ({ surveyDataView }) => {
const [startingPage,setStartingPage] = useState(null);
const [option,setOption] = useState();
const handleClick = async () => {
const result = await axios({
url: `/api/survey/${sessionStorage.getItem(
"survey_id"
)}/results/advanced_query?startingPage=${startingPage}&dataView=${option}`,method: "get",headers: {
"Content-Type": "application/json",Authorization: "Bearer " + sessionStorage.getItem("token"),},});
};
return (
<div className="results-position-list">
<h5>
<b>Select Survey Data View to Print</b>
</h5>
<select
onChange={(e) => setOption(e.currentTarget.value)}
id="position-list"
className="form-control"
>
<option></option>
{surveyDataView.map(({ surveydataview,min }) => (
<option key={min} value={surveydataview}>
{surveydataview}
</option>
))}
</select>
<InputGroup>
<InputGroupAddon addonType="prepend">
<InputGroupText className="input-text">
Enter Starting Page Number
</InputGroupText>
</InputGroupAddon>
<Input
type="number"
min={0}
max={100}
step="1"
onChange={(event) => setStartingPage(event.target.value)}
/>
<InputGroupAddon addonType="append">
<Button
onClick={handleClick}
disabled={
startingPage > 0 && option !== undefined && option !== ""
? false
: true
}
>
Generate Section
</Button>
</InputGroupAddon>
</InputGroup>
</div>
);
};
export default AdvancedQuerySurveyDataView;
from django.urls import path
from .views import (
GeneratePDFView
)
app_name = 'Results'
urlpatterns = [
path('results/advanced_query',GeneratePDFView.as_view(),name='generate_pdf')
]
解决方法
好吧,这使它在params
中使用axios
再次起作用:
const handleClick = async () => {
const result = await axios({
url: `/api/survey/${sessionStorage.getItem("survey_id")}/results/advanced_query`,method: "get",headers: {
"Content-Type": "application/json",Authorization: "Bearer " + sessionStorage.getItem("token"),},params: {
startingPage: startingPage,dataView: option
}
});
};
非常奇怪,以前没有它就可以工作。