You need to enable JavaScript to run this app. AngularJS调用PHP更新表单时响应返回undefined的问题排查
优惠活动
大模型
产品
解决方案
定价
更多

AngularJS调用PHP更新表单时响应返回undefined的问题排查

问题原因&修复方案

核心问题梳理

  • PHP端构造完响应数组后没有输出JSON内容,前端无法接收到返回数据
  • 前端$http错误回调没有声明response参数,报错时无法正常读取返回内容
  • 提交更新时传递的参数取值错误,模板中编辑的是循环内的users对象,而非$scope根层级的id和Name
  • 编辑按钮为submit类型,触发表单默认提交行为会导致页面刷新/请求异常
  • 前端成功回调直接把用户列表$scope.names赋值为响应内容,会覆盖原有列表数据

具体修复代码

1. update.php 修复

在PHP代码末尾添加JSON输出逻辑:

<?php
    try {
        header('Access-Control-Allow-Origin: *');
        header('Content-Type: application/json; charset=utf-8');
        $link = new PDO("mysql:host=localhost;dbname=angularjs", "root", "");
        $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $response = array();
        $json = json_decode(file_get_contents("php://input"), true);
        $Sql = "UPDATE register SET Name = :Name WHERE id = :id";
        $stmt = $link->prepare($Sql);
        $stmt->bindParam(":Name", $json['Name'], PDO::PARAM_STR);
        $stmt->bindParam(":id", $json['id'], PDO::PARAM_STR);
        $stmt->execute();
        $response['success'] = "更新成功"; 
    } catch (Exception $e) {
        $response['fail'] = "错误:".$e->getMessage();
    }
    // 新增:输出JSON响应
    echo json_encode($response);
?>

2. script.js 修复

// 修改Edit方法,接收循环项的users参数
$scope.Edit = function(user) {
  $http.post("update.php", {
    'id' : user.id,
    'Name': user.Name
  })
  .then(function(response){
      // 不覆盖names列表,把提示信息存到对应变量
      $scope.success = response.data.success;
      $scope.fail = '';
      console.log(response.data.success);
   }, function(response){ // 新增response参数
      $scope.fail = response.data.fail;
      $scope.success = '';
      console.log(response.data.fail);
   })
}

3. template.html 修复

<div class="container" ng-controller="welcomeController">
    <h1>{{ name }}</h1>
    <p>{{ success }}</p>
    <p>{{ fail }}</p>
    <div class="table-responsive">
        <table class="table table-hover table-borderless">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>DOB</th>
                    <th>Edit</th>
                    <th>Delete</th>
                </tr>
            </thead>
            <tbody ng-repeat="users in names">
                <tr>
                    <form>
                        <td><input type="text" ng-model="users.id" readonly></td>
                        <td><input type="text" ng-model="users.Name"></td>
                        <td>{{ users.Email }}</td>
                        <td>{{ users.DOB }}</td>
                        <!-- 修改按钮类型为button,传递当前users对象到Edit方法 -->
                        <td><button type="button" ng-click="Edit(users)" class="btn btn-success">Edit</button></td>
                        <td><button type="button" ng-click="Delete(users)" class="btn btn-danger">Delete</button></td>
                    </form>
                </tr>
            </tbody>
        </table>
    </div>
</div>

内容的提问来源于stack exchange,提问作者Suraj TheDeveloper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 05:36:03