我接手了一个项目,对JQuery和会话存储并不完全熟悉。问题是,在更新信息并单击更新后,它会提供一个错误,"Uncaught SyntaxError:JSON中位置0处的意外令牌uat JSON.parse (<anonymous>)
at fillArray (main.js? [sm]:313)
at UpdateProduct (main.js? [sm]:234)
at HTMLButtonElement.onclick ((index):405)".
包含github repo链接:https://github.com/bveasey/Jquery-project
function UpdateProduct() // called on click of an update btn
{
// capture id from hidden element set in editproduct function
var productId = $('#txtHiddenId').val();
console.log("***** line:233 *****n product id =" + productId);
fillArray(productId);
console.log("***** line:235 *****n Fill array called!");
BindTable();
console.log("***** line:237 *****n Bind Table called!");
}
// other functions
function fillArray(prodid)
{
console.log("***** line:310 *****n Begin Fill Array");
// find the item in the product array and update it
$.each(JSON.parse(sessionStorage.setItem('products',
JSON.stringify(productArray))), function (idx, v)
{
console.log("***** line:315 *****n " + product.id);
// Check if current prodid is the id wanted
if (product.id === prodid)
{// ids match
console.log("***** line:319 *****n product is's match");
// populate data
product.id = productId;
product.arrivalDate = $('#modalRequestedArrivalDate').val();
product.productCode = $('#modalProductCode').val();
product.description = $('#modalDescription').val();
product.quantity = $('#modalQuantity').val();
product.quantityType = $('#modalQuantityType').val();
product.plant = $('#modalPlant').val();
product.shippingMethod = $('#modalShippingMethod').val();
product.specialInstructions = $('#modalSpecialInstructions').val();
console.log("***** line:310 *****n END Fill Array");
}
});
}//绑定表只是更新表。
Storage#setItem()
返回void
,不能使用JSON.parse()
解析void
,void
不是有效的JSON语法。
错误告诉您问题代码的确切位置,
$.each(JSON.parse(sessionStorage.setItem('products', JSON.stringify(productArray))), function (idx, v)...
这里的sessionStorage.setItem()
不返回JSON.parse()
可以解析的任何内容。