2017-10-08 51 views
0

正在试图挽救端点URL,钥匙和令牌到数据库,但我得到一些错误而使代码下来PLZ检查如何存储在数据库中的网络推端点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

时无法receieve端点,重点,在php文件标记

+0

寻找解决方案!下面的代码作品答案,即使我认为我的代码可能工作,但真正的问题我的服务器不接受url作为我的PHP文件的参数,所以我通过替换var –

回答

0

<?php 
 
    var_dump($_POST); die; 
 

 
\t session_start(); 
 
\t if (isset($_SESSION['user_id'])) { 
 
\t $userid = $_SESSION['user_id']; 
 
\t } 
 
\t else { 
 
\t $userid =""; 
 
\t } 
 
\t // CONNECT TO THE DATABASE 
 
\t include_once("php_includes/conn.php"); 
 
\t $_POST = json_decode(file_get_contents('php://input'), true); 
 
\t $endpoint = $_POST['endpoint'] ; 
 
\t $key = $_POST['key'] ; 
 
\t $token = $_POST['token'] ; 
 
\t $axn = $_POST['axn'] ; 
 

 
\t $user_likes = mysqli_query($conn,"INSERT INTO endpointurl (endpoint,p256dh,auth) VALUES ('$endpoint','$key','$token')"); 
 
\t if ($conn->query($user_likes) === TRUE) { 
 
\t \t echo "New record created successfully"; 
 
\t } else { 
 
\t \t echo "Error: " . $user_likes . "<br>" . $conn->error; 
 
\t } 
 
?>
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(){ 
 
\t var endpoint = 'User Your Value' ; 
 
\t var key = 'Replace it with your value' ; 
 
\t var token = 'replace this one also' ; 
 
\t var axn = "subscribe" ; 
 
\t $.ajax({ 
 
\t \t type: "POST", 
 
\t \t url: "post.php", 
 
\t \t dataType : "html", 
 
\t \t data:{ 
 
\t \t "endpoint" : endpoint, 
 
\t \t "key" : key, 
 
\t \t "token" : token, 
 
\t \t "axn" : axn 
 
\t }, // Data sent to server, a set of key/value pairs (i.e. form fields and values) 
 
\t \t cache: false,    // To unable request pages to be cached 
 
\t \t success: function(data){ 
 
\t \t \t console.log(data) ; 
 
\t \t \t $("#responce").html(data); 
 
\t \t } 
 
\t }); 
 
} 
 

 
</script>

+0

中的一些字符串变通解决了服务器端PHP文件?我没有得到任何回报价值!并且数据库中没有数据 –

+0

只需将javascript代码替换为正确,就可以验证::将此添加到您的push.php的顶部。后续代码var_dump($ _ POST);死;在您的PHP脚本中,以验证您是否获得您的输入。 –

+0

没有结果,idk什么错了,你可以请再次检查代码,或者我应该更新整个文件的代码,我发布了所需的部分。没有登录控制台 –

0

您是手动的multipart/form-data编码您的要求,然后要求PHP为JSON解码它。

+0

如果我不使用该json解码行我的PHP它返回错误406值不可接受! –

相关问题