TypeError:无法读取angularjs中未定义的属性“ param”

时间:2018-11-20 10:06:53

标签: php angularjs json

$scope.productDetails = {};
$scope.getProductDetailById = function(arg) {

  var data = $.param({
    id: arg
  });

  $http({
    method: 'POST',
    url: 'http://' + $scope.url + '/scripts/viewProductById.php',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded;'
    },
    data: data
  })
  .then(function(response) {
    $scope.productDetails = response.data;
    $location.path('/single_product');
  },
  function(response) {
    //alert('wrong');
  })
}

在控制台中获取此错误:

  

TypeError:无法读取未定义的属性'param'

这是我用PHP编写的后端代码,我检查了邮递员,并在json中获取数据:

session_start();
require 'db.php';
$id = $_POST['id'];
$sql = "SELECT * FROM product WHERE id='$id'";
$qur = $connection->query($sql);
$r = mysqli_fetch_assoc($qur);
$msg[] = array("id" => $r['id'],"name" => $r['name'],"category" => $r['category'],"material" => $r['material'],
"pattern" => $r['pattern'],"occasion" => $r['occasion'],"length" => $r['length'],"width" => $r['width'],
"wash_care" => $r['wash_care'],"color" => $r['color'],"stock" => $r['stock'],"new_price" => $r['new_price'],
"old_price" => $r['old_price'],"delivery_in" => $r['delivery_in'],"image_one" => $r['image_one'],
"image_two" => $r['image_two'],"image_three" => $r['image_three'],"image_four" => $r['image_four'],
"image_five" => $r['image_five'],"description" => $r['description'],"date" => $r['date']);

$json = $msg;

header('content-type: application/json');
echo json_encode($json);
@mysqli_close($connection);

更改后:

var data = {
id: arg
  };

获取空数据

enter image description here

1 个答案:

答案 0 :(得分:0)

您不必像这样使用它。

var data = $.param({
    id: arg
});

数据变量可以像这样是Object Literal

var data = {
    id: arg
};