Sab*_*hra 5 http angularjs angularjs-1.6
我是新手AngularJs并在网络表单中使用它
我的代码如下
var app = angular.module('demoApp', [])
app.controller('usrController', function ($scope, $http, $window) {
$scope.userdata = {};
var post = $http({
method: "POST",
url: "Index.aspx/GetData",
dataType: 'json',
data: {},
headers: { "Content-Type": "application/json" }
}).success(function (data, status) {
console.log(data);
$scope.userdata = data.d;
}).error(function (data, status) {
$window.alert(data.Message);
});
});
Run Code Online (Sandbox Code Playgroud)
我的 WebMethod 代码如下
[WebMethod]
public static string GetData()
{
DataTable dt = Helper.UserList("sp_GetSearchList");
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(dt).ToString();
return JSONString;
}
Run Code Online (Sandbox Code Playgroud)
我的 JSONString 返回完美的 json 但我得到了错误
TypeError: $http(...).success 不是函数
我在堆栈溢出上看到了很多答案,但没有一个真正解决了这个问题。
使用以下代码后,它解决了我的问题
var app = angular.module('demoApp', [])
app.controller('usrController', function ($scope, $http, $window) {
$scope.userdata = {};
$http.post("Index.aspx/GetData", {}).
then(function (response) {
console.log(JSON.parse(response.data.d));
$scope.userdata = JSON.parse(response.data.d);
});
},
function(error) {
});
Run Code Online (Sandbox Code Playgroud)
我的前端绑定是这样的
<body data-ng-app="demoApp">
<form id="form1" runat="server">
<div data-ng-controller="usrController">
<table>
<tr>
<th>Sl No</th>
<th>User ID</th>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Birth Date</th>
</tr>
<tr data-ng-repeat="data in userdata">
<td>{{data.ID}}</td>
<td>{{data.UserID}}</td>
<td>{{data.EmployeeID}}</td>
<td>{{data.EmpName}}</td>
<td>{{data.BirthDate}}</td>
</tr>
</table>
</div>
</form>
</body>
Run Code Online (Sandbox Code Playgroud)
数据完美输入,console.log但在前端没有绑定。
帮我哪里错了。提前致谢
在Angular 1.6 中,$http服务发生了变化:您不能再使用.success和.error了。
相反,使用.then. 从$http文档:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
Run Code Online (Sandbox Code Playgroud)
应该是这样的
$http({
method: "POST",
url: "Index.aspx/GetData",
dataType: 'json',
data: {},
headers: { "Content-Type": "application/json" }
}).then(function(result) {
//Success
}, function(error) {
//Error
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4756 次 |
| 最近记录: |