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

时间:2017-10-08 17:06:15

标签: php json ajax web-push

尝试将端点网址,密钥和令牌保存到数据库但是收到的代码很少,导致代码关闭plz检查

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文件中的端点,密钥,令牌

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
<?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;
	}
?>
&#13;
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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你手动multipart / form-data编码你的请求,然后要求php让Json解码它。