1

I have a python script file that returns a json

{"message": "Login or password is empty", "success": 0}

I want to go to this url in html and dislpay "Login or password is empty" and also save the success int to be used later for pass fail verification.

I have no idea how to get the json parsed so that I can use it in html.

This is my html

<h2> Login</h2>
<form action="http://test.com/cgi-bin/login.py" method="POST" target="hiddenFrame2">
Username:<br>
<input type="text" name="username">
<br>
Password:<br>
<input type="password" name="password">
<br>
<br>
<input type="submit" value="Submit">
<br>
<iframe src="about:blank" frameborder="0" width="100" height="50" scrolling="no" name="hiddenFrame2" class="hide"></iframe>
<br>
<br>
</form> 
Bytesized
  • 133
  • 1
  • 15

1 Answers1

0

Suppose you have form like this,

<h2> Login</h2>
<form action="http://test.com/cgi-bin/login.py" method="POST" target="hiddenFrame2" id="myform">
Username:<br>
<input type="text" name="username">
<br>
Password:<br>
<input type="password" name="password">
<br>
<br>
<input type="submit" value="Submit">
<br>
<iframe src="about:blank" frameborder="0" width="100" height="50" scrolling="no" name="hiddenFrame2" class="hide"></iframe>
<br>
<br>
</form>
<span id="log"></span>

You can submit your form like this using ajax,

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script type="application/javascript">
$(document).ready(function(){
  $("#myform").submit(function(){
      var formvars = $("#myform").serialize();
      var url = "http://test.com/cgi-bin/login.py";

      $.ajax({
        url: url,
        dataType: 'text',
        data : formvars,
        type:"POST",
        success: function(data){
         alert( data );
         var json = eval(data);
         var success = json.success;
         var message = json.message;
         if(success =="1")
           $("#log").text(message); // Success
         else
           $("#log").text(message); // Failed
       },
       error: function(data){
        alert('error; '+ data);
       }
    });
    return false;
  });
});
</script>
Bytesized
  • 133
  • 1
  • 15
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33