weixin_33739646 2014-10-12 13:21 采纳率: 0%
浏览 96

在python中访问Ajax数据

I am newbie in python and am using Python to get the request from AJAX calls but I am getting the code of python as it is inside the AJAX output which I don't want.

AJAX Code :

$.ajax({
        type: "POST",
        url: "hello.py",
        datatype : "json",
        data: { name: "nitin"}
      })
      .done(function(msg) {
           console.log(msg);
           $("div").html("Data : "+msg)
});

Python Code :

import sys
import cgi

sys.stdout.write("Content-Type: application/json")
sys.stdout.write("
") 
sys.stdout.write("
")

form = cgi.FieldStorage()

sys.stdout.write(json.dumps({ 'data': form.getvalue('name')}))

Output : Data : import sys import cgi sys.stdout.write("Content-Type: application/json") sys.stdout.write(" ") sys.stdout.write(" ") form = cgi.FieldStorage() sys.stdout.write(json.dumps({ 'data': form.getvalue('name')}))

Expected Output should be :

Data : Content-Type: application/json

nitin
  • 写回答

2条回答 默认 最新

  • weixin_33736832 2014-10-12 13:36
    关注

    is your python script marked as executable? see this -

    "The Python script is not marked as executable. When CGI scripts are not executable most web servers will let the user download it, instead of running it and sending the output to the user. For CGI scripts to run properly on Unix-like operating systems, the +x bit needs to be set. Using chmod a+x your_script.py may solve this problem."

    there is also a sample to test if your server supports CGI -

    https://docs.python.org/2/howto/webservers.html#simple-script-for-testing-cgi

    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员