Я пытаюсь сохранить URL-адрес, ключ и токен конечной точки в базе данных, но получаю несколько ошибок, оставляя код неработающим.
main.js
var endpoint = sub.endpoint ;
var key = btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('p256dh')))) ;
var token = btoa(String.fromCharCode.apply(null, new Uint8Array(sub.getKey('auth')))) ;
var axn = "subscribe" ;$.ajax({
type: "POST",
url: "push_endpoint_save.php",
dataType : "json",
data:{
"endpoint" : endpoint,
"key" : key,
"token" : token,
"axn" : axn
}, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: "application/json; charset=utf-8", // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false
success: function(data){
console.log(data) ;
}
});
push.php
var_dump($_POST); die;
session_start();
if (isset($_SESSION['user_id'])) {
$userid = $_SESSION['user_id'];
}
else {
$userid ="";
}
// CONNECT TO THE DATABASE
include_once("php_includes/conn.php");
$_POST = json_decode(file_get_contents('php://input'), true);
$endpoint = $_POST['endpoint'] ;
$key = $_POST['key'] ;
$token = $_POST['token'] ;
$axn = $_POST['axn'] ;
$user_likes = mysqli_query($conn,"INSERT INTO endpointurl (endpoint,p256dh,auth) VALUES ('$endpoint','$key','$token')");
if ($conn->query($user_likes) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $user_likes . "<br>" . $conn->error;
}
и это структура базы данных
введите описание изображения здесь
не могу получить конечную точку, ключ, токен в файле php
<?php
var_dump($_POST); die;
session_start();
if (isset($_SESSION['user_id'])) {
$userid = $_SESSION['user_id'];
}
else {
$userid ="";
}
// CONNECT TO THE DATABASE
include_once("php_includes/conn.php");
$_POST = json_decode(file_get_contents('php://input'), true);
$endpoint = $_POST['endpoint'] ;
$key = $_POST['key'] ;
$token = $_POST['token'] ;
$axn = $_POST['axn'] ;
$user_likes = mysqli_query($conn,"INSERT INTO endpointurl (endpoint,p256dh,auth) VALUES ('$endpoint','$key','$token')");
if ($conn->query($user_likes) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $user_likes . "<br>" . $conn->error;
}
?>
TryIt<script src="https://code.jquery.com/jquery-2.2.4.min.js" ></script>
<input type="submit" name ="this is an example" value= "Try clicking me Now" onclick="justTrying()">
<br>
<br>
<div id="responce"></div>
<script>
function justTrying(){
var endpoint = 'User Your Value' ;
var key = 'Replace it with your value' ;
var token = 'replace this one also' ;
var axn = "subscribe" ;
$.ajax({
type: "POST",
url: "post.php",
dataType : "html",
data:{
"endpoint" : endpoint,
"key" : key,
"token" : token,
"axn" : axn
}, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
cache: false, // To unable request pages to be cached
success: function(data){
console.log(data) ;
$("#responce").html(data);
}
});
}
</script>
Вы вручную кодируете запрос multipart / form-data, а затем запрашиваете у php Json его декодировать.