【发布时间】:2017-07-02 13:03:10
【问题描述】:
您好,我正在尝试使用 AnguarJs 发布数据,但遇到了一些问题。我的 js 代码如下:
var app = angular.module('myApp', []);
app.constant('appUrl', 'http://localhost/zara/rest/');
/*---- categories fetch code -------*/
app.controller('myController',['$scope','$http','appUrl', function($scope, $http , appUrl) {
$scope.newName = "";
$scope.postForm = function() {
var data = $.param({
name: $scope.newName
});
$http.post("http://localhost/zara/rest/api_customerlogin.php", data).success(function(data, status) {
$scope.hello = data;
console.log(data);
});
};
}]);
我收到此错误TypeError: Cannot read property 'param' of undefined 当我将代码更改为喜欢此代码时,我收到此错误。
<script>
angular.module('myApp', [])
.controller('myController', ['$scope', '$http', function($scope, $http) {
this.loginForm = function() {
var user_data='user_email=' +this.inputData.email+'&user_password='+this.inputData.password;
$http({
method: 'POST',
url: 'login.php',
data: user_data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
console.log(data);
})
}
}]);
</script>
无法发布到 login.html 页面。这是我在 AnguarJs 上的第一个项目,所以对这个不太熟悉。谢谢。
【问题讨论】:
-
你在服务文件中试过了吗:app.serivce('service',[$http,function(){ }])
-
不,我刚刚尝试了上面的代码。服务将如何处理这个?
-
$.param究竟为您做什么?!将您的数据更改为object为var data = {name: $scope.newName} -
此错误是因为与
jquery和jquery.param相关的$未定义 -
如果你在 Angular 中使用
$http作为服务会更好。
标签: javascript php angularjs