What is server-side scripting ajax - javascript

Hi I'm new to programming and making a project exactly like these Real time GPS Tracker on JUST HTML / JS and Google Maps to be run on a handphone
My Next step is all users should see others location and one of the answers said
you'd have to send the points to a server-side script using AJAX.
I know how ajax works i just don't know what he meant by server-side script
$.ajax({
url: "sample.php",
context: document.body,
success: function(){
$(this).addClass("done");
}
});
My Questions is:
Is the code above consider a server-side script? if no any examples
Should i make new .php file?

You have a file that exist in you server so one cannot access it using their browesers.In your case that test.html file is residing in server. it can be .php file .js file ....

The code above is not a server side scripting language as the url is still point to a .html page.
A server side scripting language is a language that runs on the server rather than on the client. example of a server side scripting language is php.

Related

Call Python scripts thanks to JS or HTML

Is there any way or tips to call a Python file (or functions) winthin a .js file (called on a HTML web page).
Indeed, I did the following tip :
- configure my server (it's Apache) to run Python files (CGI)
- created a Python file on the following structure :
print("<head>....</head>")
print("<body>....</body>")
But what I want is to call a Python file (a real one, with classes, functions or other things) on the Javascript. Any ways to do it ?
(something with Ajax or other stuffs)
Thanks, Clément
EDIT
Here's what I want to achieve :
my structure :
1) HTML/CSS/JS files to create an interactive chat
2) a Python file to process the answers on the chat
My goal
On each post on the chat, a Javascript function of my .js file is called. This function need to run my Python file, and my Python file is supposed to return something. The Python file is a pure Python file, no HTML or other things.
What I've done
I created a server thanks to XAMPP. Moreover and as I said before, I tried the thing with print("...") on each line, but it's by far not what I want.
EDIT 2
I already tried a thing with AJAX like :
$(".mytext").on("keyup", function(e){
if (e.which == 13){
var text = $(this).val();
if (text !== ""){
insertChat("you", text);
$(this).val('');
$.ajax({
type: "POST",
url: "test.py"
});
}
}
});
but I get an error :
The error I get is on the HTML console, in French :
Erreur d’analyse XML : mal formé
Emplacement : http://localhost/test/test.py
Numéro de ligne 1, Colonne 2
so in English:
Error of XML analysis : malformed
Path : http://localhost/test/test.py
Line 1, column 2
JavaScript runs in your browser. Python cannot run in your browser.
The Javascript code can create requests to a server. On the server you can run all kinds of code, including python. The python script(s) have to reside on the server and the server has to be configured accordingly (see WSGI, CGI, "How to use python on the web").
Edit:
Answer to updated question:
What you want is to create XmlHttpRequests from JavaScript to the python chat program running on the server. The data format will be JSON (not XML). This technique is called AJAX ("Asynchronous javascript and XML"). I suggest you read some tutorials on these topics. The web framework for Python is called Django.

Loading an XML locally into JavaScript without having a web server available?

I've made an script for processing XML:
Extracts parts of the XML-data and displays them on a HTML-/CSS-page.
Currently I use the stringified content of some arbitrary file for doing my development. Written as a string-literal into my JavaScript.
Works great. But now comes the problem:
Of course I would like to load whatever (equal-structured) XML-files. Instead of having it in my code as a string-literal.
Normally I would load the files into my script via Ajax.
But I can't install a web-server on these computer.
I'm within an enterprise and it isn't possible to install any additional software. Restricted via group-policies etc. No chance. Forget it!
As far as I know it isn't possible to use Ajax without a web-server because Ajax communicates via the http-protocol.
So here's my questions:
I there (perhaps) a possibility to use Ajax without a web-server?
And in case of impossible:
Have I got any Ajax-alternatives to load XML-data into my script?
You can embed your xml inside script tag in your html like this:
console.log(document.getElementById('file').innerHTML)
<script type="text/xml" id="file">
<root><foo><bar></bar></foo></root>
</script>

How to upload an image file using only javascript and ajax

In form, There is a field of file
<input type="file" name="file">
<input type="button" value="upload">
I want to upload image file onto server but I can't able to upload the image. Is there any solution in only javascript? Please help me to find this answer.
Thanks..!
This isn't possible with JavaScript alone, you will need to use a server side language to process the actual uploading of the file.
This will do the job in Firefox, IE does not support FormData, so you should find another way
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<form id="data">
<fieldset>
<div>Asset File: <input id="image_file" name="image_file[]" type="file" /></div>
<div><input type="submit" value="Submit"></div>
</fieldset>
</form>
<script>
$(function() {
$("form#data").submit(function(event){
event.preventDefault();
var url = 'http://server.com/upload';
var image_file = $('#image_file').get(0).files[0];
var formData = new FormData();
formData.append("image_file", image_file);
$.ajax({
url: url,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (status) {
// do something on success
}
});
return false;
});
});
</script>
Your web site files are stored at the server. JavaScript runs at the client-side.
The images must be stored somewhere. But the client-side JavaScript have access to the user's browser window (or tab). and user's cache. You cannot write files at the server storage using only client-side JavaScript.
The ways images are uploaded and downloaded:
Download:
You send request to the server using JavaScript (ajax for example). In this request you say: "GET http://my-site/images/cool-dog.png" This is static request that try to access images/cool-dog.png from your public folder on the server. Every server has option that allows you to determinate which folder will contain all the files for the static requests.
STATIC request is when you try to access a file with an extension (cool-dog.png)
Upload:
As we know, everybody can write client-side JavaScript to the console: Every major browser has debugging tools. And everybody can send any kind of request to your server from Postman for example.
It will be bad to accept all of there request. Somebody may want to upload 100GB file at max speed. That may affect the application and server performance or even hack the application.
You need server-side logic to determinate which file is good for your server and where this file must be stored, since the JavaScript know only about client-side storage (cookies, localStorage, sessionStorage, cache, etc...).
Upload process is:
You send request from the client-side JavaScript to the server-side. For example : POST http://my-site/uploadImage with the image data.
The server-side accepts that request. For example: Router.get('uploadImage', function() {...Server side code...}). Since http://my-site/uploadImage is dynamic path (we do not have extension) The server-side Router will wait for this kind request and if it's requested the server-side code must check the file size and extension (.png or .dll) and etc... And if the file is good for your application then you can write this file to the server location.
The different server languages and technologies has different methods for processing requests and writing files.
If you want to extend your knowledge you need to learn at least one server-side language and technology. You cannot make flexible applications only with client-side logic.
If you are familiar with JavaScript yet. You may want to learn NodeJS. This site contains great tutorials for NodeJS beginners.
PHP is also easy to learn if you can found some good tutorials.
Here is tutorial how to upload files using nodeJS
and here is another for PHP

Ajax put to add XML entries into XML file

I am trying to add some emails taken from an inputbox into a .txt file present on my webserver. Here is the code :
email = document.getElementById("mail").value;
$.ajax({
url: 'maillist.txt',
datatype: 'text',
type: 'PUT',
data: email + '; ',
success: function(data) {
alert('Should have work yay!');
}
});
but that doesn't work on any browser. :(
I have tried using javascript classic methods but it was a no go as well...
I would need either a PUT or POST method, either jQuery or JS, to be able to do this on internet explorer 8 and up as well as firefox and chrome. Emails should appear in the text file as
email1#cooldomain.com; email2#cooldomain.com; .....
Just so it works with our in-house VBA Macro. :)
Also, could there be a method for dropping data into XML files (aka create a new XML entry with form data)? And also, is it possible to upload a file from client side to server side using jQuery? Because i would need users to fill up forms and drop their data into an XML file, and link up a file they choose with that. That way they could add stuff into the XML themselves and they would show up brand new into the webpage.
Kindoff "reddit" or "4chan" like if you know the references.
Thanks for your time, really appreciated!
You can't post from a browser to a text file on the server side. You need to have some sort of code on the server side that will receive the HTTP PUT, and persist the data to a file local to the server.

How do you execute a server-side Python script using jQuery?

I have a very simple Python file, called python1.py, whose contents are:
f = open('C:\\Temp\\test.txt', 'w')
f.write('Succeeded')
f.close()
I wish to execute this from JavaScript, like so:
jQuery.ajax({
type: "POST",
url: "/cgi-bin/python1.py",
success: function (msg) {
alert("Data Saved: " + msg);
}
});
However, all that happens is that I get an alert showing me the contents of the Python script. The file C:\Temp\test.txt does not get created, so clearly the Python was not executed.
How do I persuade the code to execute the Python script instead of just reading it?
You simply need to configure your web server to execute your *.py scripts, instead of serving them as plain text.
If you are using Apache as a web server, you need to enable mod_python or mod_wsgi.
EDIT:
Since you are using using Apache, you may want to check the following article, which briefly describes how to set up the mod_python module:
A Brief Introduction to Apache's mod_python Module
You could also use the opensource project Pico. It's a really elegant way of calling server side Python code from client side Javascript.
The author has provided some simple examples here https://github.com/fergalwalsh/pico/wiki/Example-1:-Hello-World
Are you able to execute the script directly from the browser. This looks more like a webserver config issue than jquery's
If your script is that simple, you would be best off using CGI on the server side rather than mod_python or mod_wsgi as suggested by others. For details on how to set up Apache for CGI with Python and simple script examples see:
http://webpython.codepoint.net/cgi_tutorial

Categories