类型错误:无法读取属性 'jquery'未定义的 [英] TypeError: Cannot read property 'jquery' of undefined
问题描述
我在 Chrome 控制台中收到以下错误,
I'm getting the following error in Chrome console,
TypeError: Cannot read property 'jquery' of undefined
这是我的 javascript 函数,
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?
推荐答案
$scope.formData.imageFile
似乎评估为 undefined
.
$.param(undefined) //throws Cannot read property 'jquery' of undefined
在 $http()
调用之前执行 console.log($scope.formData)
.$scope.formData
对象很可能不包含 imageFile
属性,或者 imageFile
为 null
/>未定义
.
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
.
注意 $.param
接受一个普通对象或数组作为第一个参数,提供另一种类型的值(或没有值)属于未定义行为.
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.
此外,如果我正确阅读了您的代码,则您正在尝试通过 $http
上传图像文件.它不会那样工作,你需要 XHR2 的 FormData
API.请参阅此答案以获得简单的实现.
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 不支持 <input type="file">
上的 ng-model
,这就是前一个错误的原因.
Angular does not support ng-model
on <input type="file">
, which is the reason for the former error.
这篇关于类型错误:无法读取属性 &#39;jquery&#39;未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!