英文:
Flask Api working Half the time- works flawlessly when sending post with python but not with javascript
问题
以下是翻译好的部分:
Hi I created an api that scrapes a website and returns values as a JSON file using python flask
我创建了一个 API,使用 Python Flask 爬取一个网站并以 JSON 文件的形式返回数值。
For simple testing I set up a web Interface where the JSON is returned using that same API and it works flawlessly, but when I use the Javascript Fetch function in my react app to call it I keep getting different erros on the server side.
为了进行简单的测试,我设置了一个 Web 界面,在该界面上使用相同的 API 返回 JSON 数据,它运行得非常顺利。但是,当我在我的 React 应用中使用 JavaScript 的 Fetch 函数来调用它时,我一直在服务器端收到不同的错误。
This is they python code that workds perfectly
这是 Python 代码,它完美运行。
and here is the javascript code that is not working properly
这是 JavaScript 代码,它没有正常工作。
They are sending the exact Same request but getting different reponses
它们发送了完全相同的请求,但收到了不同的响应。
For refrence Here is the github repo with my API
供参考,这是我的 API 的 GitHub 仓库链接。
https://github.com/amaheshwari01/POWERSCRAPER
EDIT: My Javascript code is written with react
编辑:我的 JavaScript 代码是使用 React 编写的。
英文:
Hi I created an api that scrapes a website and returns values as a JSON file using python flask
For simple testing I set up a web Interface where the JSON is returned using that same API and it works flawlessly, but when I use the Javascript Fetch function in my react app to call it I keep getting different erros on the server side.
import requests
import json
data = {
'act': '',
'pw': '',
'req': 'grades',
}
response = requests.post('http://127.0.0.1:5000/', data=data)
print(response.text)
This is they python code that workds perfectly
and here is the javascript code that is not working properly
useEffect(() => {
fetch('http://127.0.0.1:5000/', {
method: 'POST',
body: new URLSearchParams({
'act': '',
'pw': '',
'req': 'grades'
})
}).then(response => {
console.log('Response:', response)
return response.json();
}).then(response => console.log(response))
}, []);
They are sending the exact Same request but getting different reponses
What it should look like(what I get when running the python code
What happens when I run the Javascript code
For refrence Here is the github repo with my API
https://github.com/amaheshwari01/POWERSCRAPER
EDIT: My Javascript code is written with react
答案1
得分: 0
看着你的API,你需要一个表单提交。尝试更新你的JavaScript代码,使用FormData对象,看看是否可以工作。
const formData = new FormData();
formData.append('act', '');
formData.append('pw', '');
formData.append('req', 'grades');
fetch('http://127.0.0.1:5000/', {
method: 'POST',
body: formData
})
.then(response => {
console.log('Response:', response)
return response.json();
}).then(response => console.log(response))
英文:
Looking at your API you are expecting a Form post. Try updating your JavaScript to use the FormData object and see if that works.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const formData = new FormData();
formData.append('act', '');
formData.append('pw', '');
formData.append('req', 'grades');
fetch('http://127.0.0.1:5000/', {
method: 'POST',
body: formData
})
.then(response => {
console.log('Response:', response)
return response.json();
}).then(response => console.log(response))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论