简体   繁体   English

Javascript Fetch : Error Unexpected token < in JSON at position 0

[英]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.

相关问题 获取错误:-S SyntaxError: Unexpected token &lt; in JSON at position 0 REACT - Fetch Error :-S SyntaxError: Unexpected token < in JSON at position 0 REACT Javascript JSON解析错误:位置1的JSON中出现意外的令牌 - Javascript JSON parsing error: Unexpected token ' in JSON at position 1 语法错误:JSON 中位置 0 处的意外标记 p 在提取中 - SyntaxError: Unexpected token p in JSON at position 0 In fetch 提取-SyntaxError:JSON中位置0处的意外令牌&lt; - Fetch - SyntaxError: Unexpected token < in JSON at position 0 错误:位置 0 处的 JSON 中出现意外标记 &lt; - Error: Unexpected token < in JSON at position 0 使用Javascript将字符串转换为对象(错误:JSON中位置1处的意外令牌t) - Converting string to object with Javascript (Error: Unexpected token t in JSON at position 1) I'm trying to fetch my json site to enable API, but but got an error “Unexpected token &lt; in JSON at position 0” - I'm trying to fetch my json site to enable API, but but got an error “Unexpected token < in JSON at position 0” Javascript 承诺:意外令牌 &lt; 在 JSON 中 position 9 - Javascript Promises: Unexpected token < in JSON at position 9 500(内部服务器错误)和 SyntaxError: Unexpected token &lt; in JSON at position 0 when using fetch function - 500 (Internal Server Error) and SyntaxError: Unexpected token < in JSON at position 0 when using fetch function 错误:语法错误:意外的标记{位于JSON中的位置4 - Error : SyntaxError: Unexpected token { in JSON at position 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM