Javascript in web2py view

164 views
Skip to first unread message

Sif Baksh

unread,
Sep 25, 2014, 11:07:43 AM9/25/14
to web...@googlegroups.com
I have this script that I've used with PHP and it works, but I'm moving everything from PHP to Web2PY easy to deploy to coworkers.

PHP page (I left out the PHP part of the code that makes a REST API call to an appliance)

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Get Current Running Config</title>
</head>
<body>
<h2>Get Current Running Config</h2>
Choose a Device Name<br>
<select name="selectFrequency" id="selectFrequency">
  <option value="">- Select -</option>
<?php foreach ($json_a['devices'] as $devname)
{
 echo "<option value=". $devname['DeviceID'].">". $devname['DeviceName'] ."</option>";

} ?>
</select>
<div id="resultip"></div>
<script>
$(document).ready(function(){
$("#selectFrequency").change(function() {
    var selected = $(this).val();
    $.ajax({
        url: "get.php", 
        type: "GET", 
        data: {id : selected},
        success: function(data) {
          $('#resultip').html(data);
        }
    });
});
});
</script>
</body>
</html>

Here is my view page:
Enter code here...<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Get Current Running Config</title>
</head>
<body>
<h2>Get Current Running Config</h2>
Choose a Device Name<br>
<select name="selectFrequency" id="selectFrequency">
    <option value="">- Select -</option>
{{for tmp_name,tmp_id in zip(names,id):}}
    <option value="{{=tmp_id}}">{{=tmp_name}}</option>";
{{pass}}
</select>
<div id="resultip"></div>
<script>
$(document).ready(function(){
$("#selectFrequency").change(function() {
    var selected = $(this).val();
    $.ajax({
        url: "get_config_details",
        type: "GET",
        data: {id : selected},
        success: function(data) {
          $('#resultip').html(data);
        }
    });
});
});
</script>
</body>
</html>


I get the drop down populated correctly with all the device names.
But when I select a device name it calls get_config_details with ?id=43  I changed it using the inspect element in the browser to get_config_details/43 still nothing shows up on the page.
Any advice or am I approaching this incorrectly

Thanks in advance
Sif

Cliff Kachinske

unread,
Sep 26, 2014, 5:47:08 PM9/26/14
to web...@googlegroups.com
First thing, read up on the ajax functionality in Web2py. It's way simpler than jQuery ajax and will work just fine for what you want to do.l

Consider using the ":eval" target in Web2py ajax. It will allow you to send JavaScript snippets back to the server. That way you can do things like this on the server side:

Function get_config_details():
   
try:
       
# Your code goes here
       
return "$('#resultip').html(data);
    except:
        return "
$('.flash').text('Server has experienced an error. Please refresh the page.');"


Also:

What does the url going back to the server look like? 


Use the network tab in Firebug or Chrome developer tools to watch the conversation between browser and host.

Your JavaScript looks OK to me except I'm suspicious about that url. Or maybe your Web2py code is raising an exception.

Sif Baksh

unread,
Oct 1, 2014, 10:12:08 PM10/1/14
to web...@googlegroups.com
Cliff,
Thanks for the insight,

{{extend 'layout.html'}}

<h2>Get Current Running Config</h2>
Choose a Device Name<br>
<form>
<select id="q" name = "q" onchange="ajax('{{=URL('get_config_details')}}',['q'],'target');">
    <option value="">- Select -</option>
{{for tmp_name,tmp_id in zip(names,id):}}
    <option value="{{=tmp_id}}">{{=tmp_name}}</option>";
{{pass}}
</select>
</form>
<br/>
<div id="target"></div>

def get_device_conf():
    import requests
    import json
    object_type = "devices/index"
    data = requests.get(url + object_type, verify=False, auth=('admin', 'admin'))
    json_input = data.text
    try:
        decoded = json.loads(json_input)
        names = [entry['DeviceName'] for entry in decoded['devices']]
        id = [entry['DeviceID'] for entry in decoded['devices']]

        return dict(names=names, id=id)

    except (ValueError, KeyError, TypeError):
        return "JSON format error"
Reply all
Reply to author
Forward
0 new messages