【问题标题】:TypeError: Cannot read property 'param' of undefined in angularjsTypeError:无法读取angularjs中未定义的属性'param'
【发布时间】:2019-04-22 18:07:27
【问题描述】:
$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: 无法读取未定义的属性“参数”

这是我用 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
  };

获取空数据

【问题讨论】:

  • param$ 的一个属性,它是未定义的。 $ 是什么? jQuery?你有它的脚本吗?
  • 不,只是我使用了 jquery 库和 angular.min.js
  • 我调用了很多函数,但只有这个函数我得到了这样的结果。
  • 你不能直接打电话给$http.post('http://'+ $scope.url +'/scripts/viewProductById.php', { id:arg })吗?
  • 但在我的管理仪表板项目中使用相同的代码

标签: php angularjs json


【解决方案1】:

你不必像这样使用它。

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

数据变量可以只是一个Object Literal,就像这样。

var data = {
    id: arg
};

【讨论】:

  • 状态为 200 但数据为空
  • @Chit 取决于您的后端。例如使用 PHP,请仔细检查您是否使用 echo json_encode(...)
  • 已上传,请查看
  • 仍然需要帮助
猜你喜欢
  • 2015-08-26
  • 2016-09-12
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 2014-12-24
相关资源
最近更新 更多