Skip to content

Instantly share code, notes, and snippets.

@boatgm
Last active December 13, 2015 22:38
Show Gist options
  • Select an option

  • Save boatgm/4985449 to your computer and use it in GitHub Desktop.

Select an option

Save boatgm/4985449 to your computer and use it in GitHub Desktop.
跨域支持
function do_sum(param1, param2, cfunction) {
// send ajax response to server
$.ajax({
type: 'POST',
url: 'http://www.script-tutorials.com/demos/301/api.php',
crossDomain: true,
dataType: 'json',
data: 'action=sum¶m1=' + param1 + '¶m2=' + param2,
success: function(json) {
// and evoke client's function
cfunction(json);
}
});
}
function do_sub(param1, param2, cfunction) {
// send ajax response to server
$.ajax({
type: 'POST',
url: 'http://www.script-tutorials.com/demos/301/api.php',
crossDomain: true,
dataType: 'json',
data: 'action=sub¶m1=' + param1 + '¶m2=' + param2,
success: function(json) {
// and evoke client's function
cfunction(json);
}
});
}
function do_mul(param1, param2, cfunction) {
// send ajax response to server
$.ajax({
type: 'POST',
url: 'http://www.script-tutorials.com/demos/301/api.php',
crossDomain: true,
dataType: 'json',
data: 'action=mul¶m1=' + param1 + '¶m2=' + param2,
success: function(json) {
// and evoke client's function
cfunction(json);
}
});
}
function do_div(param1, param2, cfunction) {
// send ajax response to server
$.ajax({
type: 'POST',
url: 'http://www.script-tutorials.com/demos/301/api.php',
crossDomain: true,
dataType: 'json',
data: 'action=div¶m1=' + param1 + '¶m2=' + param2,
success: function(json) {
// and evoke client's function
cfunction(json);
}
});
}
<?php
// set possibility to send response to any domain
header('Access-Control-Allow-Origin: *');
if (version_compare(phpversion(), '5.3.0', '>=') == 1)
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
else
error_reporting(E_ALL & ~E_NOTICE);
// accept POST params
$sAction = $_POST['action'];
$iParam1 = (int)$_POST['param1'];
$iParam2 = (int)$_POST['param2'];
// perform calculation
$iResult = 0;
switch ($sAction) {
case 'sum':
$iResult = $iParam1 + $iParam2;
break;
case 'sub':
$iResult = $iParam1 - $iParam2;
break;
case 'mul':
$iResult = $iParam1 * $iParam2;
break;
case 'div':
$iResult = $iParam1 / $iParam2;
break;
}
// prepare results array
$aResult = array(
'result' => $iResult
);
// generate result
header('Content-type: application/json');
echo json_encode($aResult);
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="http://www.script-tutorials.com/demos/301/api.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// execute method 1 (sum) by server
var param1 = 5;
var param2 = 10;
do_sum(param1, param2, function(data) {
$('#results').append(param1 + ' + ' + param2 + ' = ' + data.result + '
');
// execute method 2 (sub) by server
param1 = 25;
param2 = 15;
do_sub(param1, param2, function(data) {
$('#results').append(param1 + ' - ' + param2 + ' = ' + data.result + '
');
// execute method 3 (mul) by server
param1 = 8;
param2 = 5;
do_mul(param1, param2, function(data) {
$('#results').append(param1 + ' * ' + param2 + ' = ' + data.result + '
');
// execute method 4 (sub) by server
param1 = 33;
param2 = 11;
do_sub(param1, param2, function(data) {
$('#results').append(param1 + ' / ' + param2 + ' = ' + data.result + '
');
});
});
});
});
});
</script>
<div id="results"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment