当我调用提交函数时,它调用但是给出这样的错误,这个函数中的错误是什么。
Error: $ is not defined
$scope.submit@http://localhost/angularAjax/app.js:19:21
Mc/u/<@https://code.angularjs.org/1.0.3/angular.min.js:70:297
dc[c]</</</<@https://code.angularjs.org/1.0.3/angular.min.js:140:1
Sc/this.$get</e.prototype.$eval@https://code.angularjs.org/1.0.3/angular.min.js:86:306
Sc/this.$get</e.prototype.$apply@https://code.angularjs.org/1.0.3/angular.min.js:86:412
dc[c]</</<@https://code.angularjs.org/1.0.3/angular.min.js:140:505
pc/c/<@https://code.angularjs.org/1.0.3/angular.min.js:22:460
m@https://code.angularjs.org/1.0.3/angular.min.js:6:191
pc/c@https://code.angularjs.org/1.0.3/angular.min.js:22:433
这是代码:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$scope.hello = {name: "Boaz"};
$scope.newName = "";
/*$scope.sendPost = function() {
var data = $.param({
json: JSON.stringify({
name: $scope.newName
})
});
$http.post("/echo/json/", data).success(function(data, status) {
$scope.hello = data;
})
} */
$scope.submit = function () {
$http({
method : 'POST',
url : 'pro.php',
data : $.param($scope.contact), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
}) .success(function (data) {
alert(data);
console.log(data);
});
}
});
Angular正在运行,只是ajax调用失败
答案 0 :(得分:1)
您在$.param
内调用的$scope.submit
函数来自jQuery。确保在页面中包含对jQuery的引用
答案 1 :(得分:0)
我认为您可以使用JSON.stringify
将数据转换为json:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$scope.hello = {name: "Boaz"};
$scope.newName = "";
/*$scope.sendPost = function() {
var data = $.param({
json: JSON.stringify({
name: $scope.newName
})
});
$http.post("/echo/json/", data).success(function(data, status) {
$scope.hello = data;
})
} */
$scope.submit = function () {
$http({
method : 'POST',
url : 'pro.php',
data : JSON.stringify($scope.contact), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
}) .success(function (data) {
alert(data);
console.log(data);
});
}
});