i am working on an python GUI application, and this part is back end of it. i am really unknown to JSON. and familiar with little bit of PHP. if possible, please give me the exact line of code where should i change it.
code giving SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data my local php version is v 8.1.12 is this a problem? ?
<?php
header('Content-type: application/json');
// Include database connector
include 'database_connector.php';
// test connection
if (isset($_POST['test_connection'])) {
$data['connection_response'] = "OK";
}
// check app key and id
if (isset($_POST['checkapp'])) {
$data['valid_app'] = False;
if (!isset($_POST['app_key']) or empty($_POST['app_key'])) {
$data['response_message'] = "Invalid app key";
}elseif (!isset($_POST['app_id']) or empty($_POST['app_id'])) {
$data['response_message'] = "Invalid app id";
}else{
if (checkAppKey($_POST['app_id'], $_POST['app_key'])) {
if (checkAppKeyExpiration($_POST['app_key'])) {
$data['response_message'] = "App key expired! Please login";
}else{
$data['response_message'] = "Valid app key and id";
$data['valid_app'] = True;
$userDetails = getAppUser($_POST['app_id']);
if(!empty($userDetails) && isset($userDetails['user_name'])){
$data['username'] = $userDetails['user_name'];
}else{
$data['username'] = "";
}
$appDetails = getAppDetails($_POST['app_id']);
if(!empty($appDetails) && isset($appDetails['key_expiration_date'])){
$data['expiry_date'] = $appDetails['key_expiration_date'];
}else{
$data['expiry_date'] = "unknown";
}
}
}else{
$data['response_message'] = "Invalid app id or key";
}
}
$data['app_id'] = $_POST['app_id'];
$data['app_key'] = $_POST['app_key'];
}
// view if app key already exists
function view_app_key($link)
{
global $dbh;
$sql = "SELECT * FROM `applications` WHERE `application_key`='".$link."'";
$query = $dbh->prepare($sql);
$query->execute();
$result = $query;
if ($row = $result->fetch( PDO::FETCH_ASSOC )){
return "true";
}else{
return "false";
}
}
echo json_encode($data);
?>
the full code is here: https://drive.google.com/file/d/1Q5qLAZW5qachxWuc4ibtDMCH5KochjuS/view
CodePudding user response:
In your ajax call try this Simply change,
var respons = jQuery.parseJSON(response);
to
var respons = response;
Explanation: If the configuration of your AJAX call is having dataType: json you'll get a JavaScript object so it's no longer necessary to use JSON.parse().