2014-11-08 151 views
0

submitform()不接收數據到函數。 Chrome控制檯說ReferenceError:$未定義。代碼中有什麼錯誤? enter image description here

var app = angular.module('app', []); 
 
    app.controller('testimonialController', function($scope, $http) { 
 
     $scope.formdata = {}; 
 
     $scope.submission = false; 
 
     $scope.submitform = function($scope, $http) { 
 
     $http({ 
 
      method: 'POST', 
 
      url: 'sendmail.php', 
 
      data: $.param($scope.formdata), // 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() { 
 
      console.log("send successfuly"); 
 
     }). 
 
     error(function() { 
 
      console.log("It is failed"); 
 
     }); 
 
     }; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div id="contact-form" ng-controller="testimonialController" ng-app="app"> 
 
    <h1 id="contact">Contact</h1> 
 
    <pre>Form data: {{formdata}}</pre> 
 
    <p>Take a look around the site, Do you have something in mind you would like to let me know .You can contact me by filling the form below or email [email protected]</p> 
 
    <form ng-submit="submitform()"> 
 
    <div class="form-wrap"> 
 
     <label for="name">Name</label> 
 
     <input type="text" name="name" ng-model="formdata.name" placeholder="Name" required> 
 
     <br> 
 

 
    </div> 
 
    <div class="form-wrap"> 
 
     <label for="email">Email</label> 
 
     <input type="email" name="email" ng-model="formdata.email" placeholder="Email" required> 
 
     <br> 
 

 
    </div> 
 
    <div class="form-wrap"> 
 
     <label for="comment">Message</label> 
 
     <br> 
 
     <textarea name="comment" id="comment" ng-model="formdata.comment" placeholder="Comment" cols="30" rows="10" required></textarea> 
 
     <br> 
 

 
    </div> 
 
    <input type="submit" name="submit" value="Send"> 
 
    </form> 
 
</div>
這是當使用 enter image description here

$http({ 
    method : 'POST', 
    url  : 'sendmail.php', 
    data : $scope.formdata, // 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) 
}). 
+0

爲什麼你使用'data:$ .param($ scope.formdata)'而不是'$ scope.formdata'? – Jonas 2014-11-08 02:20:55

+0

@Jeyp我剛剛按照這個教程http://webdevmatters.wordpress.com/2014/07/28/processing-a-form-with-angularjs-php/ – Muhammed 2014-11-08 02:26:32

+0

@Jeyp控制檯錯誤是** TypeError:無法讀取屬性' formdata'undefined **當$ .param作爲'data:$ scope.formdata'移除時 – Muhammed 2014-11-08 02:31:20

回答

1

data: $.param($scope.formdata)

這行使用jQuery的$.param方法控制檯消息。你必須包括它,如果你想使用它

+0

我沒有在函數中使用jQuery – Muhammed 2014-11-08 02:27:49

+0

@MuhammedAthimannil - 是的。我引用你鏈接的文章:'好吧,我會說實話,這裏有一個快速的作弊。我使用jQuery的$ .param來幫助我發送數據。 – Jonas 2014-11-08 12:08:00

+0

任何不使用jQuery的解決方案? – Muhammed 2014-11-08 16:22:52