javascript - 无法使用 angularjs php 将数据从 php 脚本传递到 angularjs
问题描述
当我尝试从 php 脚本更新表单响应时显示未定义。
脚本.js
$scope.Edit = function() {
$http.post("update.php", {
'id' : $scope.id,
'Name': $scope.Name
})
.then(function(response){
$scope.names = response.data;
console.log(response.data.success);
}, function(){
console.log(response.data.fail);
})
}
更新.php
<?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'] = "successfully inserted";
} catch (Exception $e) {
$response['fail'] = "Error.".$e;
}
?>
模板.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>
<td><button type="submit" ng-click="Edit()" class="btn btn-success">Edit</button></td>
<td><button type="submit" ng-click="Delete()" class="btn btn-danger">Delete</button></td>
</form>
</tr>
</tbody>
</table>
</div>
</div>
我需要成功更新的 php 脚本的响应,但它显示未定义。我尝试调试很多次,但看不到我犯了什么错误。
解决方案
推荐阅读
- google-sheets - 试图从 TODAY() 函数中减去一个值会产生错误的结果
- django - 如何使用 ajax 在模态中显示 django 通用详细视图
- java - 蛇是怎么用这些代码移动的?
- python - 如何在 python-docx 中正确编写多个 XML 参数
- java - 如何通过 Monitor Used Object 理解 GC Root?
- javascript - 谷歌自定义搜索引擎无法正常工作
- google-cloud-platform - 禁用 Stackdriver 日志记录 GCP
- python - 连续分布的 scipy.stats 属性“熵”不能手动工作
- python - python中的“pspectrum”函数
- shopify - 如何在产品页面中添加自定义选择?