複数のJavaScript関数でいくつかのajax呼び出しがあります。それぞれが/ajaxを使用して複数のPHP関数を呼び出す方法、ベストプラクティス?
のfunctions.phpはJS、例えばJavaScriptの
からのものと対応している必要があり、複数の機能を持っていfunctions.php
ファイルにポストを得るん私が持っている:
function one() {
$.ajax({
type: 'POST',
url: 'http://www.example.com/functions.php',
data: vals,
dataType:'json',
success: function (data) {
alert(data);
}
});
}
function two() {
$.ajax({
type: 'POST',
url: 'http://www.example.com/functions.php',
data: othervals,
dataType:'json',
success: function (data) {
alert(data);
}
});
}
function three() {
$.ajax({
type: 'GET',
url: 'http://www.example.com/functions.php',
data: { get_param: 'user' },
dataType:'json',
success: function (data) {
alert(data);
}
});
}
PHPで私はこのようなものがあります:
if (isset($_POST['city'])) { // this comes from the javascript vals json object
$city = $_POST['city'];
function one() {
// do something
return true;
}
}
function two() {
// do something
return true;
}
if (isset($_GET['get_param']) && $_GET['get_param'] == 'user'){
function three() {
// do something
return true;
}
}
は多分、PHP側はビットが私はそれを書く方法を混同しているのが、最終的に、私はをしたいです210はphpファイルからのcorespondent関数を処理するだけです。明らかに、彼らは同じ名前を持つ必要はありません。
、PHPの関数はtrue
または
false
または
1
または
0
を返すことができて、私は私が知っていると病気に明確にしてください何をしたいの詳細混乱がある場合には、警告データ
alert(data);
すると仮定
ありがとう
これは、 'callBackend([" user "、" comments "、5])'のようなもので、バックエンドのいくつかの関数に基づいて最新の5コメントを得ることができます。 – Michael