我试图将一些数据发布到php,但它不起作用,我也检查了chrome dev工具。当我点击相等按钮时,它将显示以下状态: calculation.php 方法:OPTIONS 状态:(已取消) 纯文本/ jquery.min.js:4 0 B 8毫秒
我不确定是什么问题。但它可以在这里工作:daciusa.org/pella/calc.html
$(function () {
var $show = $('#display');
var currentDisplay = "";
$show.val(0);
$(document).on('click', 'button.number', function () {
if ($show.val().length >= 8) {
$show.val("Error");
} else if ($show.val() == "Error") {
$show.val("0");
} else if ($.inArray($show.val(), ['+', '-', 'x', '/']) !== -1) {
var addOp = $show.val();
if (addOp) memory.push(addOp);
$show.val($(this).val());
} else {
$show.val(($show.val() == "0" ? "" : $show.val()) + $(this).val());
}
});
$("#clear").click(function () {
$show.val("0");
});
$("#ce").click(function () {
if ($show.val().length >= 2) {
$show.val($show.val().substring(0, $show.val().length - 1));
} else {
$("#ce").trigger("click");
}
});
var memory = [];
$(document).on('click', 'button.function', function () {
var addnum = $show.val();
if (addnum) memory.push(addnum);
$show.val($(this).val());
});
$('#equ').click(function () {
var addnum = $show.val();
memory.push(addnum);
$.post("calculation.php", {
execute: memory
}, function (data, status) {
console.log("Success!!");
console.log(data);
console.log(status);
$show.val(data);
var e = memory.join('');
memory = [];
});
});
});
php
<?php
if(isset($_POST['execute']) && is_array($_POST['execute'])) {
$total = (int)$_POST['execute'][0];
for($i=1;$i<count($_POST['execute']);$i+=2){
switch($_POST['execute'][$i]){
case '+': $total += (int)$_POST['execute'][$i+1];break;
case '-': $total -= (int)$_POST['execute'][$i+1];break;
default : $total += (int)$_POST['execute'][$i+1];
}
}
echo $total;
}
else echo 'Error';
?>
答案 0 :(得分:0)
你应该试试
echo json_encode($total);
而不是
echo $total;
答案 1 :(得分:0)
可以尝试:
JS
$(function () {
var $show = $('#display');
var currentDisplay = "";
$show.val(0);
$(document).on('click', 'button.number', function () {
if ($show.val().length >= 8) {
$show.val("Error");
} else if ($show.val() == "Error") {
$show.val("0");
} else if ($.inArray($show.val(), ['+', '-', 'x', '/']) !== -1) {
var addOp = $show.val();
if (addOp) memory.push(addOp);
$show.val($(this).val());
} else {
$show.val(($show.val() == "0" ? "" : $show.val()) + $(this).val());
}
});
$("#clear").click(function () {
$show.val("0");
});
$("#ce").click(function () {
if ($show.val().length >= 2) {
$show.val($show.val().substring(0, $show.val().length - 1));
} else {
$("#ce").trigger("click");
}
});
var memory = [];
$(document).on('click', 'button.function', function () {
var addnum = $show.val();
if (addnum) memory.push(addnum);
$show.val($(this).val());
});
$('#equ').click(function () {
var addnum = $show.val();
memory.push(addnum);
$.ajax({
type: "POST",
url: "calculation.php",
dataType: "json",
data:{
execute: memory
}
}).done(function(response) {
if (response.status == 'successful'){
console.log("Success!!");
console.log(response.data);
console.log(response.status);
$show.val(response.data);
var e = memory.join('');
memory = [];
} else {
console.log("failed!");
}
}).fail(function() {
console.log("failed!");
console.log(arguments);
});
});
});
PHP
<?php
$output = array(
'status' => 'unsuccessful'
);
if(isset($_POST['execute']) && is_array($_POST['execute'])) {
$total = (int)$_POST['execute'][0];
for($i=1;$i<count($_POST['execute']);$i+=2){
switch($_POST['execute'][$i]){
case '+': $total += (int)$_POST['execute'][$i+1];break;
case '-': $total -= (int)$_POST['execute'][$i+1];break;
default : $total += (int)$_POST['execute'][$i+1];
}
}
$output['status'] = 'successful';
$output['data'] = $total;
}
header('Content-Type:application/json');
echo json_encode($output);