0

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

2 Answers 2

1

The $.param function you are calling inside $scope.submit is from jQuery. Make sure you are including a reference to jQuery in your page

Sign up to request clarification or add additional context in comments.

2 Comments

I'm new in angularJS and getting $.param is not defined, Can you elaborate your solution?
Angularjs has a jQuery lite version built in. This apparently is a function from the main jQuery library. You have to add jQuery into the app before angulajrs.
0

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);               
                });  
                }

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.