“如何修复”;JSON输入意外结束“;在python CGI脚本和javascript之间
我一直收到“SyntaxError:JSON输入的意外结束” 但我想不出原因 我使用此javascript代码向python脚本发送2个数字,并从python接收json应答“如何修复”;JSON输入意外结束“;在python CGI脚本和javascript之间,javascript,python,json,Javascript,Python,Json,我一直收到“SyntaxError:JSON输入的意外结束” 但我想不出原因 我使用此javascript代码向python脚本发送2个数字,并从python接收json应答 const requestData = (num1, num2) => { let number1 = JSON.stringify(num1); let number2 = JSON.stringify(num2); //here I send the data to the c
const requestData = (num1, num2) => {
let number1 = JSON.stringify(num1);
let number2 = JSON.stringify(num2);
//here I send the data to the cgi python script. But instead of a json answer I get this error
const data = fetch(`./cgi-bin/test.cgi?num1=${number1}&num2=${number2}`)
.then(response => {return response.json() //the error occurs here: "SyntaxError: Unexpected end of JSON input"
})
.catch(err => console.error(err))
}
requestData(2, 3);
这是我的python脚本,应该是
从javascript代码接收两个数字,并将答案转换为json并发送回
#!/usr/bin/env python3
import json
import cgi
def sum(num1, num2):
solution = num1 + num2
return {"solution": solution}
# here I read the data I have received from javascript
parameters = cgi.FieldStorage();
num1 = json.loads(parameters.getValue('num1'));
num2 = json.loads(parameters.getValue('num2'));
answer = sum(num1, num2)
# here I send an answer back to javascript
print("Content-Type: application/json")
print()
# I suspect that the issue might be with this code. Maybe it doesn't properly convert to json?? but I'm not sure
print(json.dumps(answer))
我不明白错误的原因。
谢谢大家帮助我 使用DevTools中的“网络”选项卡查看完整响应。