1

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\r\n\r\n\n"
print

print url
f = open('/home/stack/writing.txt','wb')
f.write(url)
f.close()
1
  • Your data: is in the wrong format. It needs to be in json format, which is '{"key": "value"}' Commented Jul 8, 2014 at 17:12

1 Answer 1

1

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);
   },
});
Sign up to request clarification or add additional context in comments.

1 Comment

I can't seem to write the response in the File now or print it anywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.