Reputation: 196
When i call submit function it calls but give error like this , what is the error in this function .
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
Here is code :
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 is working , just ajax call fails
Upvotes: 0
Views: 5238
Reputation: 2370
i think you can convert the data to json, using JSON.stringify
like this:
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);
});
}
});
Upvotes: 0
Reputation: 86
The $.param
function you are calling inside $scope.submit
is from jQuery. Make sure you are including a reference to jQuery in your page
Upvotes: 1