[英]Javascript Fetch : Error Unexpected token < in JSON at position 0
I am sending a POST request to my Django view.我正在向我的 Django 视图发送 POST 请求。 When I run the code in local it works but when I run it on my apache server it gives me a 500 error.
当我在本地运行代码时,它可以工作,但是当我在我的 apache 服务器上运行它时,它给了我 500 错误。 Could you help me, please !!
请问你能帮帮我吗 !!
This is my code:这是我的代码:
form.addEventListener('submit', e=>{
e.preventDefault()
const baseInput = document.querySelector('#inputbaseform0')
if(baseInput.value !== null){
$('#loadMe').modal('show');
let data = {}
data['base'] = baseInput.value
data['tiempo'] =tiempo.value
data['otros'] =otros.value
let url = "{% url 'cal' %}";
fetch(url, {
method: "POST",
credentials: "same-origin",
headers: {
"X-CSRFToken": document.querySelector('#form0 input').value,
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(data)
}).then(function(response){
return response.json();
}).then(function(data){
console.log('ERROR: ', data.error)
baseData = parseFloat(data.base).toFixed(2)
deducir.value = data.porciento//porciento
$('#loadMe').modal('hide');
}).catch(function (e){
console.log('Error', e);
$('#loadMe').modal('hide');
})
}
})
})
Your problem is not in your js code.您的问题不在您的 js 代码中。 This code does exactly what you want, trying to parse response data as json.
此代码完全符合您的要求,尝试将响应数据解析为 json。 But your django view returns the 500 error page for some reason, and this page is html starting with "<".
但是由于某种原因,您的 django 视图返回了 500 错误页面,并且该页面是以“<”开头的 html。
Logs can tell you why your django view throws an exception.日志可以告诉您为什么 django 视图会引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.