Call python FUNCTION from javascript - 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.

Related

Can AJAX and JavaScript be used purely for server side scripting?

I am just learning php. My approach to learning is that I start analysing the code in a language I am more familiar with.
Hence, I am trying to replicate a PHP dashboard with PHP features like role management, session logging etc.
As I am halfway through it, I find that using ajax , though not primarily meant for it, I can talk to a server.
Using Http verbs, I can also make modifications.
Hence, can JavaScript and Ajax together act as a server side scripting language on its own?
My understanding is a server side scripting language just interacts with the server.
As ajax does the same using JavaScript and http verbs .
Can it essentially replace PHP?
I did a little research, and found server side scripting languages have their own pros like filtering, jQuery and Ajax can use less bandwidth etc.
But everything seems vauge.
Hoping to get a better understanding.
Thank you:)
Server side scripts run at the server, not at the "client" (browser). Javascript run at the browser. Ajax is a feature of JS that allow you to interact with a server without doing a full POST back to that server (changing the page you are actually seeing). During an AJAX call you can interact with whatever is on the server side (PHP, .NET, etc) There are even some sites that have multiple server side scripts.

Using Python to communicate with JavaScript?

Is there a way to send data packets from an active Python script to a webpage currently running JavaScript?
The specific usage I'm looking for is to give the ability for the webpage, using JavaScript, to tell the Python script information about the current state of the webpage, then for the Python script to interpret that data and then send data back to the webpage, which the JavaScript then uses to decide which function to execute.
This is for a video game bot (legally), so it would need to happen in real time. I'm fairly proficient in Python and web requests, but I'm just getting into JavaScript, so hopefully a solution for this wouldn't be too complex in terms of Javascript.
EDIT: One way I was thinking to accomplish this would be to have Javascript write to a file that the Python script could also read and write to, but a quick google search says that JavaScript is very limited in terms of file I/O. Would there be a way to accomplish this?
For security reasons, javascript in a browser is usually restricted to only communicate with the site it was loaded from.
Given that, that's an AJAX call, a very standard thing to do.
You can make HTTP requests using the XMLHttpRequest API, which Jquery abstracts with $.ajax and $.get. You can also use the lower level Websockets network API:
https://developer.mozilla.org/en-US/docs/WebSockets
Note that the XMLHttpRequest API will only allow requests to the same server, OR requests that return an appropriate Access-Control-Allow-Origin header.
It sounds like the Javascript is only going to send information, not receive any. In that case, you're in luck. I'm guessing you are also running the Javascript and the Python on the same machine.
Run a Python webserver on the machine the browser is running on. Here's a simple example:
http://webpy.org/install
Once visiting http://127.0.0.1:8080/ in your browser gives the message Hello World!, you can start adding more addresses to your website, for example http://127.0.0.1:8080/report_data, http://127.0.0.1:8080/report_event etc.
Your Javascript can then make AJAX requests using jQuery.ajax or XMLHTTPRequest, to the address http://127.0.0.1:8080/report_data, and pass the information as GET parameters.

NodeJS : make requests to server and get the result

Is it possible to execute Javascript on server side and to get the result on a client ?
If it is possible, how can i make the request to the dedicated Javascript file from my client ?
In fact, i have created a mobile application with embeeded Javascript, (using Phonegap) but i would like to put all Javascript files on the server and make some requests to the server to get the result.
Thanks for your help
See You have to create a Web Service on the Server Side to enable the Client(Phonegap App) to make an Ajax request and fetch some data.
Earlier you could have written a web service using programming languages like PHP, .NET, Java, Ruby, Python etc. But NodeJS provides the flexibility of writing REST Based web services in Javascript itself.
You can learn about it here: http://appsonmob.com/nodejs-expressjs-mysql/

Easiest way to implement a simple web-service project?

I am creating an add-on for my browser that should report the websites you visit automatically to some remote server (no server response needed).
The browser extension is written in Javascript, so I suppose XHR requests are available for me to make. The only thing that comes to mind for me is to create a RESTful web-service and use that but I am thinking that there must be an easier way? or is SOAP the way to go?
If you want to go with REST and you like Java then I'd recommend jersey: http://jersey.java.net/
If you really want simple and aren't concerned with following all the REST principles you can just make a simple perl cgi or php script and just post the request to it.

Implementing a very small HTTP server in c/c++ and want to use AJAX

I want to have a dynamic webpage that automaticly updates some information, this information should be received from my c/c++ application using HTTP. I have set up a socket and can send HTML and Javascript files to the browser.
I don't know how to move on. How to encapsulate my data into XMLHttpRequest objects? Or maybe this isn't the way to go? The problem is that my c/c++ application will be run on an embedded system that can't really support php or something like that.
I can't really understand how XMLHttpRequest works, I only find a lot of client examples on the web and not much about how a server should handle it.
A server should handle it as any other request. From the servers point of view, it's a normal HTTP request. Return the data that the client asks for! This is usually a HTML fragment, some XML or some JSON.
Ajax just send normal HTTP GET POST ... request, you should make sure your response header is correct, such as Content-Type.
How do you send information to the browser? The browser is client-side. To get information, you must either query the server (which you say is written in C++). If you want your client to receive request, you should probably emulate a server-like behavior using NodeJS.

Categories

Resources