Looking for an ajax example


#1

I am trying to implement functionality where the plugin's settings javascript code calls back to the plugin's server code.

I am having trouble locating such an example. Is this possible?


#2

Definitely. But you'll have to create your own settings view model and bind that to your settings pane. Then to e.g. query a SimpleApi endpoint provided by your plugin you'd do something like this:

OctoPrint.simpleApiGet("my_plugin")
    .done(function(response) {
        // do something with the response
    })

or to send a comment to a SimpleApi:

var  payload = {"parameter": "value", "other_parameter": "other_value"};
OctoPrint.simpleApiCommand("my_plugin", "my_command", payload)
    .done(function(response) {
        // do something with the response
    })

or to query some Blueprint endpoint:

OctoPrint.get(OctoPrint.getBlueprintUrl("my_plugin") + "my_endpoint")
    .done(function(response) {
        // do something with the response
    })

See also the JS client library documentation.


#3

@foosel Thank you. How do I define the method on the server-side (e.g. in my Python code)? Do I need to decorate the method with some type of attribute to make it web-accessible?


#4

Take a look at the SimpleApiPlugin and BlueprintPlugin mixins whose documentation I linked above.