2017-12-27 481 views
0

我很猶豫要問,但我還沒有找到任何解決方案。在ajax函數中發送php文件中的數據之後,當我嘗試捕獲php文件中的數據時,數據丟失。但是,昨天我發現了一個沒有問題的類似功能。 這是AJAX部分:file_get_contents(「php:// input」)後ajax後空

$http({ 
      url: 'PDO/Companion.php', 
      data: { 
       Companion : Companion, 
       Companion : statut 
      }, 
      method: 'POST', 
      headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} 


     }).then(function (response) {// on success 
      $scope.name=undefined; 
      $http.get('js/controller/upPartenaire.php').success(function(data) { 
       $scope.result = data; 
       console.log(data); 
      }), function(msg){ 
       console.log("ça") 
       alert(msg); 
      }; 

,而PHP的一部分:

$postdata = file_get_contents("php://input"); 
$request = json_decode($postdata); 
echo json_encode($request); 

在項目中,我使用PHP只針對AJAX部分做SQL queryI也趕上與$ _ POST,但沒有數據

+0

您是否已完成盡職調查並使用瀏覽器開發人員控制檯和網絡選項卡調試了ajax調用?你是否積極發送數據,並且沒有控制檯錯誤? – IncredibleHat

+0

您還應該包含'var Companion'和'statut'定義的代碼,並解釋您爲什麼試圖將「Companion」的數據鍵名發送兩次。最後,如果您加載了jquery庫,那麼爲什麼不使用'$ .post'來簡化很多? [JQuery的.post](https://api.jquery.com/jquery.post/) – IncredibleHat

回答

0

您需要設置的內容類型,以JSON和使JSON字符串化像

$http({ 
     url: 'PDO/Companion.php', 
     data: JSON.stringify({ 
      Companion : Companion, 
      Companion : statut 
     }), 
     method: 'POST', 
     headers : {'Content-Type':'application/json; charset=utf-8'} 


    }).then(function (response) {// on success 
     $scope.name=undefined; 
     $http.get('js/controller/upPartenaire.php').success(function(data) { 
      $scope.result = data; 
      console.log(data); 
     }), function(msg){ 
      console.log("ça") 
      alert(msg); 
     }; 
+0

我試過你的解決方案,但沒有改變 –

+0

什麼是錯誤? –

+0

如果你想使用'x-www-form-urlencoded',最好的方法是使用'$ _POST ['Companion']'。另一件事是你發送同伴兩次 –

0

這應該對你有所幫助。考慮更改php代碼:

header("Content-Type: application/json"); 

parse_str(file_get_contents('php://input', false , null, -1 , 
$_SERVER['CONTENT_LENGTH']), $postdata); 

echo json_encode($postdata); 
+0

感謝您的幫助,我嘗試你的解決方案,但它不運行,我有兩個錯誤信息: 注意:未定義的索引:CONTENT_LENGTH 和 警告:json_decode( )期望參數1是字符串,第5行中給出的數組 –

+0

$ _SERVER ['CONTENT_LENGTH']應該可用,如果您發出發佈請求。此外,我會編輯我的答案更新的解決方案。 –

+0

好吧,也許PHP的部分不是問題,但阿賈克斯部分。 –