首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaScript ajax post没有在Python语言中获取数据?

JavaScript ajax post没有在Python语言中获取数据?
EN

Stack Overflow用户
提问于 2018-06-09 20:37:15
回答 1查看 74关注 0票数 0

我是网络开发的初学者。我正在尝试使用POST方法发出ajax请求(不使用jquery)。以下是我的JS代码

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])

请参阅下面的错误

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 02:30:13

您必须在ajax中添加以下标头,而不是添加json

xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50774378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档