2017-05-28 80 views
0

我有一个JavaScript变量,特定的值已分配给。我想通过角度js ajax发送变量值php变量。我的代码是follwing,请检查它并告诉我应该如何收集在PHP中的JavaScript变量值,然后我可以回显它。我在角落js ajax工作完成,但我不知道如何收集在PHP现在的价值。感谢pasing javascript变量通过角度js ajax

\t  function sendVal(event){ 
 
\t  \t var x = event.currentTarget.getAttribute('data-id'); 
 

 
\t  \t var headers = {'Content-Type': 'application/x-www-form-urlencoded'}; 
 
\t   var app = angular.module("myApp",[]); 
 
\t   app.controller("myCtrl",function($scope,$http){ 
 
\t    
 
\t    $http({ 
 
\t    \t method:"POST", 
 
\t    \t url:"files.php?data_val=true", 
 
\t    \t data:{ 
 
         data_val : x 
 
\t    \t }, 
 
\t    \t headers:headers 
 
\t    }).then(function(res){ }) 
 

 
\t   });

回答

0

你的POST请求应该以这种方式(你也附加在URL中的值作为GET,我删除它,因为我认为这是一个错误,但我不知道你有没有在PHP中一些奇怪的逻辑,看起来该变量在太):

var headers = {'Content-Type': 'application/json'}; 
$http({ 
     method:"POST", 
     url:"files.php", 
     data: { 
      "data_val": x 
     }, 
     headers:headers 
     }) 

然后在PHP你应该有这样的事情:

$json = file_get_contents('php://input'); 
$obj = json_decode($json, true); 
echo $obj["data_val"]; 
+0

我不知道为什么它不为我工作,你知道任何可以帮助我的源代码教程吗? –

+0

这是两小时前的答案:https://stackoverflow.com/questions/44230088/sending-the-id-value-of-a-div-through-angular-js-ajax-to-php-variable/ 44230127?noredirect = 1#comment75470208_44230127 – quirimmo