weixin_33693070 2013-12-17 07:25 采纳率: 0%
浏览 37

将数据发布到python脚本

I have an html page hosted at my local server, apache2, I want to send some data to a python script and manipulate it, I use the when I use the action tag of the form, It doesn't navigate to the next page and when I try Jquery the script doesn't get executed. Any help? Thanks in advance.

Html:

<body>
  <div id="Header"></div>
    <div id="wrapper">
    <form name="login-form" class="login-form" action="/cgi-bin/test.py" method="post" onsubmit="validate();return false;">
      <div class="header">
        <h1>Enter Your Credentials</h1>
        <span>URL,Admin,Tenant,Password</span>
      </div>
      <div class="content">
        <input id="Uname" name="url" type="text" class="input username" placeholder="Keystone URL" />
        <div class="user-icon"></div>
        <input id="Pname"name="admin" type="password" class="input password" placeholder="Admin" />
        <div class="pass-icon"></div>
        <input id="oPass"name="password" type="password" class="input password" placeholder="Password" />
        <div class="pass-icon"></div>
        <input id="tenant"name="tenant" type="password" class="input password" placeholder="Tenant" />
        <div class="pass-icon"></div>   
      </div>
      <div class="footer">
        <input type="submit" name="submit" value="Submit" class="button"/>
      </div>
    </form>
  </div>
</body>

This way the script gets executed perfectly but doesn't navigate to next page.

Javascript Function:

function validate() {
    var un = document.getElementById("Uname").value;
    var valid = false;

    var unArray = ["http://192.168.1.102:5000/v2.0"]; 
    for (var i = 0; i < unArray.length; i++) {
        if ((un == unArray[i])) {
            valid = true;
            break; 
        }
    }

    if (valid) {
        alert("Login was successful"); 
        location.href = 'HomePage.html';
        document.login-form.action='/cgi-bin/test.py';

        return false; 
    } else {
        alert("Login was Unsuccessful");
        return false;
    }
}

This is the Ajax call that I try to make:

$.ajax({
  type: "POST",
  url: "/cgi-bin/test.py",
  data: "stuff_for_python="+document.getElementById("Uname").value,
  success: function(response) {    
      alert(response);      
  },
  error: function(data) {
      alert(data.responseText);
  }
});

Python Script:

#!/usr/bin/python
import cgi, cgitb 
from StringIO import StringIO
import json
from io import BytesIO
import pycurl

cgitb.enable()
# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
url = form.getvalue('stuff_for_python')

print "Content-type: text/html


"
print

print url
f = open('/home/stack/writing.txt','wb')
f.write(url)
f.close()
  • 写回答

1条回答 默认 最新

  • weixin_33682719 2013-12-17 07:27
    关注

    The problem is in data value passed by post.
    try this:

    $.ajax(
    {
       type: "POST",
       url: "/cgi-bin/test.py" ,
       data: {stuff_for_python: document.getElementById("Uname").value},
       success: function(response)
       {
           alert(response);
       },
       error: function(data)
       {
           alert(data.responseText);
       },
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多