问题
I'm new to client and server side scripts, and I was wondering, how come they can't interact?
The major difference Code Conquest stated here is that:
... is called a client side language is because it runs scripts on your computer after you’ve loaded a web page.
And
A server side or back-end language runs its scripts before the HTML is loaded, not after.
Even if the server side script (e.g. PHP) has already been executed, how come it can't be changed (with JavaScript) after the page has loaded?
I want to call PHP from JS. For example, is there a possible way to do this?
setInterval(<?php someFunction() ?>,10000);
Sorry if I'm misinterpreting something, and please point it out to me.
回答1:
Well they can interact, but not like you are imagining it.
Think of client side JavaScript as the browser, and think of server side code, well as the server.
Then they would communicate by sending messages between each other, the most commonly used method for exchanging messages would be JSON.
To sum this up, client side code can communicate with the server be sending messages through GET
or POST
requests, or by using AJAX.
The server can respond to those messages, and it can also (this was added in HTML 5 standard) send events to the client using WebSockets.
回答2:
They can't interact (sort of - explained in more detail below) because they run on two different places. Server Side Scripts - eg, PHP, will run on the server and complete running before data is sent to the user's PC.
Client Side scripts (eg, Javascript) START running when the user finishes loading the page data.
So, in short, the PHP finishes executing before the JS even starts to execute - hence they can't really interact.
How you make them interact is through some trickery and calls inside the JS to request aadditional data from the server - so at that point they will still run individually, but you can get them to talk via additional calls made in the page.
Using this approach, even though they are being executed totally independantly of one another, they are able to exchange information via data (such as JSON objects) and give the impression that they are working together, although they are doing it in totally different places.
Non Technical Analogy
To use a completely non IT analogy which will hopefully make it clearer, lets say that server side scripts are an army base. Client Side scripts are units deployed into another country. They are all there to accomplish a particular task, but they work totally independantly of one another. Now, JSON is like a phone call between them. The deployed units can make a call to the base, ask for further instructions and then execute them, but as far as the base is concerned, they have no idea what is happening until the deployed units call home again - so they can SORT-OF work together, but do so individually and without knowing what the other is really doing.
回答3:
The easiest method for this is to use AJAX, it's a lightweight way of dynamically generating/finding content.
var ajax_call = function() {
$.ajax({ url: 'script.php?argument=value' }); // gets the php code you need.
};
var interval = 1000 * 60 * X; // X = amount of minutes it will take till it executes.
setInterval(ajax_call, interval); // sets the ajax_call function in motion
Let me know if this works.
回答4:
Simple example:
var request = new XMLHttpRequest();
request.open('GET', 'http://example.com/yourfile.php?func=someFunc', false);
request.send();
setInterval(request.responseText, 10000);
Or assign to var:
yourvar = request.responseText;
setInterval(yourvar, 10000);
Then in yourfile.php:
$func = $_GET['func'];
//use $func to call func or do other stuff
回答5:
Client side runs on the computer browser. Server side runs on the server and generates the code that is sent to the client.
Ajax or jQuery can fetch and send data directly from the server.
来源:https://stackoverflow.com/questions/19717668/why-cant-server-side-and-client-side-scripts-interact