javascript - 为什么我的标头没有使用 beforSend 方法设置?
问题描述
我正在尝试使用那段代码发出 ajax 请求:(我在 html 中加载的单独文件)
$(document).ready(function () {
$('#loadDeviceBtn').click(function (event) {
event.preventDefault();
createTableIfNotExist();
var device = $('#devices-search').val();
console.log(device);
getTopicsForDevice(device);
});
});
function createTableIfNotExist() {
if (!$('#table').length) {
$('.card-body').append("<div id=\"table\"><table><thead><tr><th>Topic</th><th>Value</th></tr></thead></table></div>");
}
}
function getTopicsForDevice(device) {
$.ajax({
type:"GET",
url:"api_url",
dataType: "json",
beforeSend: function (request, settings) {
console.log(settings.url);
request.setRequestHeader("Authorization", "Bearer reallylongtoken");
}
});
}
但是我不断收到 403 错误,当我查看请求标头时,我尝试设置的授权不存在。但我知道该函数被调用是因为出现了日志。
可能很重要的小细节......我的 IDE(IntelliJ)没有将 ajax 字涂成黄色,而 beforeSend 字也没有像在其他工作项目中那样使用。
解决方案
推荐阅读
- python-3.x - 自动导入子模块
- c++ - 在 C++ 中计算产生零
- linux - RPM 包下载以使用特定的提供程序
- python - 如何检查特定列中的范围是否涵盖 Pandas 中的特定值?
- reactjs - 防止导航组件在单个路由上呈现,但在所有其他路由上呈现
- java - 逐个检查球的四个边缘以查看它是否与墙壁碰撞的方法
- ansible - Ansible 在循环中创建模板并访问项目
- mysql - 在使用 SET sql 创建的表中插入和删除多个值
- python - 如何在 HTML 音频源标签中链接 Django 模型的属性?
- c++ - 有没有一种简单的方法来检查 C++ 中的不安全表达式?