[英]SyntaxError: Unexpected token S in JSON at position 17
In a product warranty registration, there is a syntax error in JSON.parse()
but I can not find this error. 在产品保修注册中,
JSON.parse()
存在语法错误,但我找不到此错误。 this is the first application in AngularJS that I am doing, usually I program in Java. 这是我正在AngularJS中进行的第一个应用程序,通常我使用Java进行编程。 I have an API developed in PHP that does the Back-end, and a controller that makes the communication between the API and the view.
我有一个用PHP开发的API,它执行后端操作,还有一个控制器,用于在API和视图之间进行通信。
My controller: 我的控制器:
function adicionarGarantiaCtrl(
$scope,
$rootScope,
constants,
connectServerFactory
ngDialog
) {
var METHOD_GET;
var METHOD_POST;
var URL_ADICIONAR_LINK_GARANTIA;
init();
function init() {
METHOD_GET = constants.METHOD_GET();
METHOD_POST = constants.METHOD_POST();
URL_ADICIONAR_LINK_GARANTIA = constants.URL_ADICIONAR_LINK_GARANTIA();
}
$scope.salvarGarantia = function() {
var salvar = {
"ano": $scope.add_ano,
"mes": $scope.add_mes,
"link": $scope.add_link,
"num_mes": $scope.add_nmes
};
console.log("TESTE adicionarGarantiaCtrl");
console.log(salvar);
connectServerFactory.conectar(
METHOD_POST,
URL_ADICIONAR_LINK_GARANTIA,
salvar,
function(data) {
console.log(data);
$rootScope.garantia_ano = data.ano;
$rootScope.garantia_mes = data.mes;
$rootScope.garantia_link = data.link;
$rootScope.garantia_num_mes = data.num_mes;
},
function(erro) {
console.log(erro);
}
);
ngDialog.close();
}
}
My API.. 我的API
function adicionar_garantia() {
$request = \Slim\Slim::getInstance()->request();
$garantia = json_decode($request->getBody());
$sql = "INSERT INTO garantia(" .
"garantia, troca_distribuidor, garantia_fabrica, garantia_antecipada" .
") VALUES (" .
":garantia, :troca_distribuidor, :garantia_fabrica, :garantia_antecipada" .
")";
try {
$db = getDB();
$stmt = $db->prepare($sql);
$stmt->bindParam("garantia", $garantia->garantia);
$stmt->bindParam("troca_distribuidor", $garantia->troca_distribuidor);
$stmt->bindParam("garantia_fabrica", $garantia->garantia_fabrica);
$stmt->bindParam("garantia_antecipada", $garantia->garantia_antecipada);
$stmt->execute();
$garantia->id_garantia = $db->lastInsertId();
$db = null;
echo json_encode($garantia);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
The issue you're having here is the JSON in the catch
block is not quoting the message. 您在这里遇到的问题是
catch
块中的JSON没有引用消息。 The following line: 下一行:
echo '{"error":{"text":'. $e->getMessage() .'}}';
Should become: 应该变成:
echo '{"error":{"text":"'. $e->getMessage() .'"}}';
Perhaps a better option would be to continue using the json_encode
function as you did in the try
section of the try catch
. 也许更好的选择是像在
try catch
的try
部分中那样继续使用json_encode
函数。 This is both (In my opinion) easier to debug, and easier to read. (在我看来)这既易于调试,又易于阅读。
echo json_encode(array(
'error' => array(
'text' => $e->getMessage()
)
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.