gpt4 book ai didi

javascript - Backbone 中出现奇怪的错误 : Uncaught SyntaxError: Unexpected token B

转载 作者:行者123 更新时间:2023-12-03 08:52:58 25 4
gpt4 key购买 nike

Uncaught SyntaxError: Unexpected token Bn.get_api_token 
@ ain.js:14s.sync
@ services.js:10i.Model.extend.sync
@ UserHeaderModel.js:13n.extend.fetch
@ backbone.js:599i.View.extend.initialize
@ UserHeaderView.js:13e.View
@ backbone.js:1192i
@ backbone.js:1852e.exports.i.View.extend.initialize
@ HeaderView.js:16e.View
@ backbone.js:1192i
@ backbone.js:1852i.Router.extend.initialize
@ router.js:14e.Router
@ backbone.js:1440i
@ backbone.js:1852a.init
@ app.js:10o.1../router
@ app.js:39r
@ _prelude.js:1t
@ _prelude.js:1(anonymous function)
@ _prelude.js:1

一切都工作正常,直到某个时候,但现在我收到这个奇怪的错误:

Uncaught SyntaxError: Unexpected token B

我的 services.js 代码:

    var $ = jQuery = require('jquery'),
Handlebars = require('handlebars'),
Backbone = require('backbone'),
mainJs = require('./main');

var services = {

sync: function (method, model, options) {
var self = this;
var api_token = mainJs.get_api_token();
var access_token;
if (api_token == null) {
access_token = api_token;

} else {
access_token = api_token.access_token;
}

options || (options = {});

var beforeSend = options.beforeSend;
options.beforeSend = function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token)
if (beforeSend) return beforeSend.apply(this, arguments);
};

switch (method) {
case "read":
options.url = options.readUrl;
break;
case "delete":
options.url = options.deleteUrl;
break;
case "update":
options.url = options.updateUrl;
options.contentType = 'application/json';
break;
case "create":
options.url = options.createUrl;
options.contentType = 'application/json';
break;
}

options.error = function (xhr, statusTxt, thrown) {
switch (xhr.status) {
case 401:
console.log("Unauthorized error");
break;
default:
var messageText = JSON.parse(xhr.responseText);;
console.log("Status code: " + xhr.status + " Error: " + messageText.message);
}
}

if (options.url)
return Backbone.sync.call(model, method, model, options);

}

}


module.exports = services;

main.js

    var mainJs = {
get_api_token: function () {
var apiat = null;
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; ++i) {
var kv = cookies[i].split("=");
if (kv[0].trim() === "API") {
apiat = kv[1].trim();
break;
}
}
if (apiat) {
return JSON.parse(apiat);
}
return null;
}
}

module.exports = mainJs;

请帮忙

最佳答案

您的 JSON.parse(apiat) 可能正在解析非 JSON 的内容。

如果你打印 apiat 你就会发现你的问题。

一个好的做法是始终JSON.parse 包装在 try {} catch () 子句中:

try {
JSON.parse(data);
} catch (e) {
console.log('Error parsing JSON', e);
}

关于javascript - Backbone 中出现奇怪的错误 : Uncaught SyntaxError: Unexpected token B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32615624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com