我最近开始学习Javascript,并且正在做一个小项目。我遇到错误,指出“$.ajax 不是函数”。 endpoint
、token
和 body
是已定义的变量。
var $ = require('jQuery');
$.ajax({
type: 'POST',
url: endpoint + token,
contentType: 'application/json',
data: JSON.stringify(body),
success: function(data, status) {
console.log(data);
console.log(status);
},
error: function(jqXHR, status, errorThrown) {
console.log(errorThrown);
console.log(status);
console.log(jqXHR);
}
});
有人发现我的东西有问题吗?
最佳答案
2个可能的原因:
- 检查 jQuery 是否已添加到您的 index.html(或主 html 的任何名称)文件中。
如果上述与您的问题无关,那么您需要检查一下,即 jQuery 是否可供您使用,如下代码所示:
if(window.jQuery) { ...do your stuff here } else{ ... handle it according to your need here }
此外,如果您要从 html 输入中获取值,请不要忘记检查您的文档对象是否已准备好。
$( document ).ready(function() { console.log( "ready!" ); });
关于javascript - $.ajax 不是一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459671/