Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  195] [ 4]  / answers: 1 / hits: 34416  / 10 Years ago, fri, april 18, 2014, 12:00:00

I'm getting the following error in Chrome console,



TypeError: Cannot read property 'jquery' of undefined


Here is my javascript function,



<script type=text/javascript>
var formApp = angular.module('formApp',[]);

function formController($scope,$http){
$scope.formData = {};

$scope.processForm = function(){
$http({
method : 'POST',
url : 'http://localhost:8080/ABCAPI/v1/image/front',
data : $.param($scope.formData.imageFile),
headers : {'Content-Type': multipart/form-data , 'Auth-Token' : X}
}).success(function(data){
console.log();

if (!data.success) {
// if not successful, bind errors to error variables

} else {
// if successful, bind success message to message
$scope.message = data.message;
}
})
};
}

</script>


What does above error means and where did I go wrong?


More From » jquery

 Answers
12

$scope.formData.imageFile seems to evaluate to undefined.



$.param(undefined) //throws Cannot read property 'jquery' of undefined


Do a console.log($scope.formData) right before the $http() call. The $scope.formData object most likely does not contain an imageFile property, or imageFile is null/undefined.



Note that $.param takes a plain object or array as the first argument, providing another type of value (or no value) falls under undefined behavior.






Also, if I'm reading your code correctly, you're trying to upload an image file through $http. It won't work that way, you need XHR2's FormData API. See this answer for a simple implementation.



Angular does not support ng-model on <input type=file>, which is the reason for the former error.


[#71405] Wednesday, April 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;