3

I am trying to run a simply backend python method from a jquery script.

The JQUERY

$(document).ready(function(){
    $('#btn').click(function(event){
    event.preventDefault();
    var commit = $("#test_form input[type='radio']:checked").val();
    alert(commit);
          $.ajax({
                type: "POST",
                url: "submitted",
                data: commit,
                success: function(data) {
                    alert(data["title"]);
                    $("#status").html(data["title"]);
                }
          });
     return false;
    });

    $('#update_help').click(function() {
        $('#update_info').toggle('slow', function() {
        // Animation complete.
        });
    });
});

In my cherrypy python script, I have the following

@cherrypy.expose
def submitted(self, commit = 0):
    cherrypy.response.headers['Content-Type'] = 'application/json'
    print"Got here"
    #return simplejson.dumps(dict(title="hello", success=True))
    return "foo"

The HTML file is like the following

<form  id="test_form" method="post">

    <li class = "option">
            <input type="radio" name="commit" value = "0"/> Option 1
        </li>
        <li>
            Option 1

        </li>
        <li class = "option">
                <input type="radio" name="commit" value = "1"/> Option 2 <br>
        </li>
        <li class = "option">
            <input id="btn" type="submit"/>
        </li>
    </ul>        
        </form>

What I notice is that the ajax post never really finds the "submitted" function. The whole page reloads and nothing ever is returned to the ajax post callback. I am sure this has something to do with me doing something wrong with the dispatching but what am I missing?

Thanks

  • which class is the submitted handler in? perhaps this line should include that class - url: "/class/submitted", – Andrew Kloos Nov 10 '12 at 18:08
  • This actually resolves the url to http://localhost:9001/plugins/set_prop/submitted., but in Chrome Inspector I see a 404 not found error. SetProp is the class. The index method in it, works just fine. – user1814696 Nov 11 '12 at 01:52
  • You see a 404 in Chrome when going to localhost:9001/plugins/set_prop/submitted or your ajax request to /submitted? – Andrew Kloos Nov 11 '12 at 12:27
  • Without knowing the URL of the page that the JS is working from, I'd change the URL to an absolute URL (ie /submitted), otherwise it will be considered relative to the path of the current page. HTH. – elarson Apr 25 '14 at 18:22

1 Answers1

0

Try updating your javascript to this...

      $.ajax({
            type: "POST",
            url: "/plugins/set_prop/submitted",
            data: commit,
            success: function(data) {
                alert(data["title"]);
                $("#status").html(data["title"]);
            }

Hope this helps. Andrew

Andrew Kloos
  • 4,189
  • 4
  • 28
  • 36