javascript - 将ajax数据发布到python函数时遇到问题
问题描述
我有一个 Chrome 扩展,其背景页面包含变量url。我正在尝试将url发送到我的 python 代码,但我在后台日志中得到“400 (BAD REQUEST)”,并且在我的 python 日志中没有错误。
背景.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("receiving URL");
console.log(request);
var articleUrl = request;
console.log(articleUrl)
// sending URL to python
$.ajax({
type: 'POST',
url: `${url}/buttoncolor`,
data: articleUrl,
success: function myFunction(data) {
console.log("just sent the url to buttoncolor")
}
})
});
应用程序.py:
@application.route('/buttoncolor', methods=['GET', 'POST'])
def buttoncolor():
print ("buttoncolor")
if request.method == 'POST':
if not request.form['articleUrl']:
flash('Please enter the url field', 'error')
else:
rurl = request.form['articleUrl']
print("here comes rurl")
print(rurl)
print ("buttoncolor") 工作正常,但 print("here come rurl") 没有出现。我在 background.js 中有另一个带有 $.ajax 的 chrome.runtime.onMessage.addListener,它可以发布并很好地应用于 application.py 中的另一个函数。
解决方案
修改你的ajax数据
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("receiving URL");
console.log(request);
var articleUrl = request;
console.log(articleUrl)
// sending URL to python
$.ajax({
type: 'POST',
url: `${url}/buttoncolor`,
data:{articleUrl: articleUrl},
success: function myFunction(data) {
console.log("just sent the url to buttoncolor")
}
})
});
推荐阅读
- c++ - 为什么文件文件名被设置为一个点,而不是在读取向量中的文件名时?
- html - 无法获取
和
- 在同一高度
- python - 将 python 请求 cookie 传递给浏览器
- r - 放大时 Mapbox 底图不显示 3D 建筑物
- java - Mapbox Navigation android Repo 克隆并且无法运行面临插件的调试编译错误
- ruby-on-rails - Rails 强参数在 ajax 发布请求中看不到参数
- express - 通过 Express.js 使用 Google Cloud 进行 CRUD 操作
- typescript - 如何防止非类 TypeScript 接口上的“@typescript-eslint/unbound method”错误?
- python - 函数字典和对象
- apache - Magento 2 全页缓存消耗服务器上的总内存