<script>
$(document).ready(function(){
$("#amount").on('click', function(){
var amount = this.value;
$.ajax({
url: "ipg-util.php/createHash",
type: 'post',
data: { "amount": amount
},
success: function(response) { console.log(response); }
});
});
});
</script>
答案 0 :(得分:0)
您可以简单地将Method to Call
作为data
属性的一部分传递:
<script>
$(document).ready(function(){
$("#amount").on('click', function(){
var amount = this.value;
$.ajax({
url: "ipg-util.php",
type: 'post',
data: { "amount": amount, "callable": "createHash"},
success: function(response) { console.log(response); }
});
});
});
</script>
然后在ipg-util.php
里面你可以这样做:
<?php
$callable = isset($_POST['callable']) ? $_POST['callable'] : "default";
$amount = isset($_POST['amount']) ? $_POST['amount'] : null;
switch($callable){
case "createHash":
$response = createHash(); // CALL createHash METHOD DEFINED HEREIN
break;
case "doAnotherThing":
$response = doAnotherThing(); // CALL doAnotherThing METHOD DEFINED HEREIN
break;
default:
$response = default(); // CALL default METHOD DEFINED HEREIN
break;
}
die(json_encode($response));
function createHash(){
}
function doAnotherThing(){
}
function default(){
}
答案 1 :(得分:0)
$。ajax 调用服务器上下文或URL,无论是调用特定的&#39;操作&#39;。您想要的是以下代码:
$.ajax({ url: '/demo/websitepage',
data: {action: 'calltofunction'},
type: 'POST',
success: function(output) {
alert(output);
}
});
在服务器端,应该读取操作POST参数,相应的值应该指向要调用的方法,下面的代码用于执行此操作:
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'calltofunction' : test();break;
case 'blah' : blah();break;
// ...etc...
}
}
希望这样可以更好地参考:https://en.wikipedia.org/wiki/Command_pattern