2017-06-17 34 views
1

我想發送一些數據到save.php爲了保存在分貝。 問題是:Ajax發佈成功,它在控制檯中返回正確的數據,但save.php中的'echo'沒有返回任何內容。我不知道這段代碼有什麼問題。Ajax發送isaute

var $nick = $('span.nick').html(); 
var $scores = points; 
var range = n2; 
var $upgrades = 
    { 
     'numbers': range, 
     'pointsPerSec': pps, 
     'multip': multi 
    }; 


    //Save to DB 
    $.ajax({ 
     url: "save.php", 
     type: "POST", 
     data: { 
      userID: $nick, 
      userPoints: $scores, 
      userUpgrades: $upgrades 
     }, 
     async: false, 
     cache: false, 
     success: function(){ 
      alert('Data saved'); 
      console.log(save); 
      getData(); 
     }, 
     error: function(){ 
      alert('You fucked up mate :('); 
     } 
    }); 



function getData(){ 
    $.ajax({ 
     type: 'GET', 
     url: 'save.php', 
     async: false, 
     cache: false, 
     success: function(data){ 
      data = $.parseJSON(data); 
      $.each(data, function(i, save){ 
       console.log(save); 
      }); 
     }, 
     error: function(){ 
      alert('Error Ajax'); 
     } 
    }); 
} 

這是我的save.php文件

if(isset($_POST['userPoints']) && !empty($_POST['userPoints'])){ 
    $post=$_POST["userPoints"] or $_REQUEST["userPoints"]; 
    $dec_post=json_decode($post); 
    echo $dec_post; 
} 

回答

0

你沒有通過的任何參數在你的Ajax成功功能,以檢索和成功功能控制檯的數據,你應該傳遞參數在成功更改阿賈克斯成功的功能

success: function(response){ 
    alert('Data saved'); 
    console.log(response); 
    getData(); 
}, 
+0

它返回0。 我該怎麼辦? – LastShadowPL

+0

@LastShadowPL檢查你的PHP腳本,我不認爲它是使用正確的語法賦值$ post變量 –

0

發現你的錯誤:

if(isset($_POST['userPoints']) && !empty($_POST['userPoints'])) 
     { 
      $post = $_POST["userPoints"] or $_REQUEST["userPoints"]; 
      $dec_post = json_encode($post); // mistake was here 
      echo $dec_post; 
// you should exit; or die; here if this is final response 
     } 

成功回調有ARG 響應你缺少

//Save to DB 
$.ajax({ 
    url: "save.php", 
    type: "POST", 
    data: { 
     userID: $nick, 
     userPoints: $scores, 
     userUpgrades: $upgrades 
    }, 
    async: false, 
    cache: false, 
    success: function(save){ 
     alert('Data saved'); 
     console.log(save); 
     getData(); 
    }, 
    error: function(){ 
     alert('You fucked up mate :('); 
    } 
}); 
+0

它的返回0 – LastShadowPL

+0

我想多數民衆贊成save.php回聲.. –

+0

你寫了'json_decode'它應該是'json_encode' –

0

這裏的save.php

session_start(); 
    /*if(!isset($_SESSION['loggined'])&&($_SESSION['loggined']==true)) { 
     header('location: index.php'); 
     exit(); 
    }*/ 
    require_once "dbconnect.php"; 
    $connect= @new mysqli($host, $db_user, $db_pass, $db_name); 
    if($connect->connect_errno != 0){ 
     die("There was an error"); 
    } 
    else 
    { 
      $nick = $_POST['userID'] or $_REQUEST["userID"]; 
      $enNick = json_encode($nick); 
      echo $enNick; 


     $points = $_POST['userPoints'] or $_REQUEST["userPoints"]; 
     $enPoints = json_encode($points); 
     echo $enPoints; 

     $upgrades = $_POST['userUpgrades'] or $_REQUEST["userUpgrades"]; 
     $enUpgrades = json_encode($upgrades); 
     echo $enUpgrades; 

$connect->close();