简体   繁体   English

语法错误:JSON中位置0处的意外令牌<-不知道为什么

[英]SyntaxError: Unexpected token < in JSON at position 0 - Not sure why

I am receiving the above mentioned error when trying to update my database. 尝试更新数据库时收到上述错误。 I read about why that might be but I am confused because I have already made the CRUD for 2 other tables in php+javascript so I was just copy pasting and rewriting for the corresponding rows, etc. (And I have already copy-pasted it multiple times to see if i just made a spelling mistake) 我读到了为什么会这样,但是我很困惑,因为我已经在php + javascript中为另外2个表制作了CRUD,所以我只是复制粘贴并重写了对应的行,等等。(而且我已经复制粘贴了它)多次查看我是否犯了拼写错误)

Every console log works until that response.json() bit. 每个控制台日志在该response.json()位起作用。

 // DELETE AND UPDATE SANDWICH document.addEventListener('click', function(e){ if (e.target.classList.contains('btnDeleteSandwich')){ const sandwichId = parseInt(e.target.dataset.id); console.log(sandwichId); fetch("../backend/api-delete-sandwich.php?id="+ sandwichId, { method:'delete' }) .then(function(response){ return response.json(); }) .then(function(response){ console.log(response); document.getElementById('sandwich'+sandwichId).remove(); }) } if (e.target.classList.contains('btnEditSandwich')){ const sandwichId = parseInt(e.target.dataset.id); let sandwich; for (let i = 0; i<allSandwiches.length;i++){ if (allSandwiches[i].id_sandwich==sandwichId){ sandwich=allSandwiches[i]; } } console.log(sandwich); // * this is for filling in the update form with existing date but disabled for the moment // const updateName = document.getElementById("updateName"); // updateName.value = pizza.name; // const updatePizzaType = document.getElementById("updatePizzaType"); // updatePizzaType.value = pizza.type; // const updatePizzaIngredients = document.getElementById("updatePizzaIngredients"); // updatePizzaIngredients.value = pizza.ingredients; // const updatePizzaNormalPrice = document.getElementById("updatePizzaNormalPrice"); // updatePizzaNormalPrice.value = pizza.n_size_price; // const updatePizzaLargePrice = document.getElementById("updatePizzaLargePrice"); // updatePizzaLargePrice.value = pizza.l_size_price; document.querySelector('#btnUpdateSandwich').setAttribute('data-id', sandwichId); } }); const btnUpdateSandwich = document.querySelector('#btnUpdateSandwich'); btnUpdateSandwich.addEventListener('click', function(){ const formUpdateSandwich = document.querySelector('#formUpdateSandwich'); const formUpdateSandwichData = new FormData(formUpdateSandwich); const sandwichId = parseInt(btnUpdateSandwich.dataset.id); console.log(formUpdateSandwichData); console.log(formUpdateSandwich); fetch("../backend/api-edit-sandwich.php?id="+ sandwichId, { method:'post', body:formUpdateSandwichData }) .then(function(response){ return response.json(); }) .then(function(response){ console.log(response); }) .catch(function(err){ console.log(err); }) }) 

I'm rather new here so if you need any extra intel to solve the issue, please let me know. 我在这里很新,所以如果您需要任何其他的英特尔来解决问题,请告诉我。

Try checking to ensure the response is okay and adding a catch to your first promise chain. 尝试检查以确保响应正常,并在第一个诺言链中添加一个陷阱。 Your api may be returning an error that isn't being caught. 您的api可能会返回未捕获的错误。 I've updated your example with code from the error handling example as specified in the MDN docs for fetch: 我已使用MDN文档中指定的错误处理示例中的代码更新了您的示例以进行提取:

document.addEventListener('click', function(e){
if (e.target.classList.contains('btnDeleteSandwich')){
    const sandwichId = parseInt(e.target.dataset.id);
    console.log(sandwichId);
    fetch("../backend/api-delete-sandwich.php?id="+ sandwichId, {
        method:'delete'
    })
        .then(function(response){
            if(response.ok) {
                return response.json();
            }
            throw new Error('Network response was not ok.');
        })
        .then(function(response){
            console.log(response);
            document.getElementById('sandwich'+sandwichId).remove();
        })
        .catch(function(error){
            console.log('There has been a problem with your fetch operation: ', error.message);
        })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM