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 三菱变频器模块怎么检测触发
  • ¥15 在C#方法中使用了.Refresh();刷新了控件,但是不是最新值
  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗