【问题标题】:Sending the id value of a div through angular js ajax to php variable通过角度js ajax将div的id值发送到php变量
【发布时间】:2017-10-29 00:44:17
【问题描述】:

我有多个具有不同 ID 的 div。我正在尝试做的是,当单击该特定 div 时,将调用一个 js 函数并将该 div 的 id 值发送到 php 并分配给 php 变量,然后将得到响应。我需要通过 angular js ajax、$http 方法来完成。这是我的简单ajax方法,我现在要做什么?

<div class="col-sm-4 col-md-4 ppp" ng-repeat="id in d" data-id="32">


function divClick(id){

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

        headers:headers
      }).then(function(res){  }
      })
  });

}

【问题讨论】:

    标签: javascript php angularjs ajax


    【解决方案1】:

    只需添加要发送的数据:

    $http({
            method:"POST",
            url:"files.php",
            data: {
              "id": id
            },
            headers:headers
          })
    

    并更改您的标题以设置为内容:application/json

    【讨论】:

    • 然后如何在 php 变量中获取它?
    • 你需要解码json对象,然后访问obj的id属性。这应该工作:$json = file_get_contents('php://input'); $obj = json_decode($json, true); echo $obj["id"];
    • 附注直接以 JSON 格式发送,这样也更容易阅读,我也更新了答案中的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2022-01-24
    相关资源
    最近更新 更多