简体   繁体   中英

Call python FUNCTION from javascript

this question have been asked numerous times, I know and I'm sorry if by ignorance I didn't get the answers.

I have a hosting plan which doesn't allow me to install django, which was really nice to call an rest api easily with the routing settings.

What I want is to be able to call a python function from javascript code doing a get/post (I'm using AngularJs, but it would be the same making an ajax get/post.)

Let's say I have a js controller 'Employee' and a view 'CreateEmployee'.

From my javascript view, I can call my CreateEmployee() on the js controller, now my question is, how can I call a specific function (let's say) def CreateEmployee(params...) on my .py file?

All I found is making a get/post on my .py file, but didn't find how to invoke a specific function.

I probably don't get the python and client/server communication paradigm, I've been coding on asp.net WebForms for a long time, and since I can't use frameworks like Django I'm stuck.

Thanks

It doesn't have much to do with Python, really. Your javascript code is executed on the client's brower, and all it can do is issuing HTTP requests (synchronous or asynchronous). At this point which webserver / technology / language is used to handle the HTTP request is totally irrelevant. So, from the client javascript code POV, you are not "calling a Python function", you are sending an HTTP request and handling the HTTP response.

If your web host doesn't let you run django (or any wsgi-compliant script) then you'll probably have to either use plain CGI (warning: very primitive techno) or migrate to PHP (no comment). Or find another hosting that doesn't live in the past ;)

You must have a way to communicate over HTTP with your server side Python code. This could help.

When you do, you communicate over HTTP. You will not invoke Python functions directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM