JavaScript ajax post没有在python中获取数据?
我是网络开发的初学者。我正在尝试使用POST方法(不使用jquery)发出ajax请求。下面是我的JS代码JavaScript ajax post没有在python中获取数据?,javascript,python,python-3.x,Javascript,Python,Python 3.x,我是网络开发的初学者。我正在尝试使用POST方法(不使用jquery)发出ajax请求。下面是我的JS代码 function loadDoc() { var Obj = {"user_Name":"testName"}; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 20
function loadDoc() {
var Obj = {"user_Name":"testName"};
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("x").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "/cgi-bin/Messapp/hostelapp.py", true);
xhttp.setRequestHeader("Content-Type", "text/json");
xhttp.send(JSON.stringify(Obj));
}
下面是我的python脚本
#!D:/python/python.exe
print("Content-Type: text/json\n\n")
import cgi, cgitb , re
cgitb.enable()
form = cgi.FieldStorage() #I am facing problem in this line
print(form["user_Name])
请查看下面的错误
您必须在ajax中添加以下标题,而不是json
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
为什么没有jQuery?为什么要将内容类型设置为json,然后发送明显不是json的内容?@DanielRoseman Post-edited。实际上,我已经发送了JSON数据,但我已经尝试使用我发布的代码中的其他一些示例。你能告诉我我的请求有什么问题吗?@roganjosh我正在学习JavaScript,这就是我尝试纯JS的原因。你能告诉我我们不能用JS实现吗?我不能告诉你这两种方式是否可以实现,但我很好奇为什么这是一个要求。如果是为了学习,那就足够了。