如何在数据库中存储 Web 推送端点 url

how to store web push endpoint url in database

我正在尝试将端点 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;
}

这是数据库结构 enter image description here

无法接收 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 解码。