Tao
Tao

Reputation: 235

How to call Pyramid server from JavaScript

I have used pyramid to write a project. In a html page of the project, i want to use a button to call a python script, the python script is located in the path "myapp/Script".

I have this method tried but it dosn't work:

<script Language="javascript">
    function call() {
        http://localhost/Scripts/xxx.py
    }
</script>

Thank you very much! :)

Upvotes: 2

Views: 343

Answers (1)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83348

In most scenarios, the communicating from JavaScript to server-side is done using AJAX. Furthermore, most sites use jQuery JavaScript library to make AJAX programming easier.

AJAX requests are handled like any other HTTP requests in Pyramid.

  1. Write a view in Pyramid (this is the "python script" part)

  2. Map this view to URL

  3. Include jQuery library on your HTML page

  4. Then call it through AJAX

    <script>
        function call() {
            $.ajax("/myurl/", function(result) {
                console.log("I got this response from the server ", result)
            });
        }
    </script>
    

Upvotes: 2

Related Questions