我有一个Django模型,我可以使用Admin界面或Swagger POST添加记录。然而,我有一个vue表单,它给出了代码400,没有其他解释。我试图使用postman,但它给出了"detail": "Unsupported media type \"text/plain\" in request."
下面是SWAGGER中使用的JSON。
{
"matchingKey": "chgpt-a",
"sequence": 1515,
"param_kwargs_json": {},
"param1": "tensorFlow",
"param2": "N/A",
"param3": "N/A",
"param4": "N/A",
"param5": "N/A",
"param6": "N/A",
"param7": "N/A",
"param8": "N/A",
"param9": "N/A",
"param10": "N/A",
"description": "test from postman 1331"
}
以下是来自SWAGGER的响应头
access-control-allow-origin: *
allow: GET, POST, HEAD, OPTIONS
content-length: 265
content-type: application/json
cross-origin-opener-policy: same-origin
date: Fri, 07 Apr 2023 06:27:13 GMT
referrer-policy: same-origin
server: WSGIServer/0.2 CPython/3.10.9
vary: Accept, Origin, Cookie
x-content-type-options: nosniff
x-frame-options: DENY
当我在Postman中使用完全相同的JSON并带有以下标题时……
request_headers= {'Content-Length': '222', 'Content-Type': 'application/json', 'Host': 'localhost:8000', 'Connection': 'keep-alive', 'Sec-Ch-Ua': '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"', 'Accept': 'application/json, text/plain, */*', 'Sec-Ch-Ua-Mobile': '?0', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36', 'Sec-Ch-Ua-Platform': '"Windows"', 'Origin': 'http://localhost:444', 'Sec-Fetch-Site': 'same-site', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Dest': 'empty', 'Referer': 'http://localhost:444/', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9'}
我对CORS的设置如下:
CSRF_TRUSTED_ORIGINS = ["http://localhost:8000", "http://localhost:444/"]
#CSRF_COOKIE_DOMAIN = '.localhost'
CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOWED_ORIGINS = [
'http://127.0.0.1:8000',
'http://localhost:8000',
'http://127.0.0.1:5173',
'http://localhost:5173',
'http://127.0.0.1:444',
'http://localhost:444'
]
我的vue表单提交方法如下:
aveParameter(_newParameters){
console.log('saveParameter()')
this.swalLoading('Adding New Parameter...')
//check for completeness
if(true){
console.log('saveParameter data = %s', _newParameters.matchingKey)
setTimeout(()=>{
// POST API FOR ADDING Parameter
getAPI.post('http://localhost:8000/api/parameters/', _newParameters)
.then(res=>{
// SETS MESSAGE FOR RESPONSE
let message = `Parameter ${res.data.requestpath} ${res.data.requestmethod} has been successfully added.`
// DISPLAYS SUCCESS SCREEN/MODAL
this.swalSuccess(message)
})
.catch(err=>{
this.swalError(err)
})
}, 1000)
}
我尝试发布时的Console.log如下:
oggleAddParameterModal
mixins.js:130 --mixins toggleModal(addParameter)
mixins.js:139 Proxy(Object) {closeModal: ƒ, …}
ParametersTableModal.vue?t=1680847383434:119 saveParameter()
ParametersTableModal.vue?t=1680847383434:123 saveParameter data = moogoogaipan
mixins.js:130 --mixins toggleModal(addParameter)
mixins.js:139 Proxy(Object) {closeModal: ƒ, …}
xhr.js:187 POST http://localhost:8000/api/parameters/ 400 (Bad Request)
dispatchXhrRequest @ xhr.js:187
xhrAdapter @ xhr.js:13
dispatchRequest @ dispatchRequest.js:53
request @ Axios.js:108
Axios.<computed> @ Axios.js:140
wrap @ bind.js:9
(anonymous) @ ParametersTableModal.vue?t=1680847383434:126
setTimeout (async)
saveParameter @ ParametersTableModal.vue?t=1680847383434:124
(anonymous) @ ParametersTableModal.vue:111
(anonymous) @ runtime-dom.esm-bundler.js:1450
callWithErrorHandling @ runtime-core.esm-bundler.js:155
callWithAsyncErrorHandling @ runtime-core.esm-bundler.js:164
invoker @ runtime-dom.esm-bundler.js:339
mixins.js:130 --mixins toggleModal(parametersTable)
mixins.js:139 Proxy(Object) {validate: ƒ, formatPrice: ƒ, formatPriceIntl: ƒ, …}
所有输入的JSON几乎都是一样的,swagger经常使用漂亮的json,\n,但除此之外都是一样的,这就是工作的原因。
我怀疑CORS,但根据我的设置,CORS不应该是一个播放器。另一个提示来自媒体类型text/plain,但没有一个头文件有任何东西,但application/JSON似乎是统一的。
我有其他的vue.js表单正在同一个应用程序上工作,所以我不知所措,也许一双新的眼睛可以帮助我找到错误。
AV
1条答案
按热度按时间ncecgwcz1#
问题出在模型上,而不是GUI。
这里是模型中正确的行,`param 2 = models.CharField(max_length=200,blank=True,null = True,default = 'N/A')
在其他工作的模型中,我将blank和null都设置为true,就像这里一样,当我更新违规模型以添加blank=True时,一切都恢复正常,错误消息具有误导性。