【问题标题】:my post method is not working in localhost我的 post 方法在 localhost 中不起作用
【发布时间】:2014-12-15 00:42:29
【问题描述】:

我正在尝试将一些数据发布到 php,但它无法正常工作,而且我已经检查了 chrome 开发工具。当我单击相等按钮时,它将出现此状态: 计算.php 方法:选项 状态:(取消) 文本/纯文本 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';
?>

【问题讨论】:

  • 你确定你有某种网络服务器正在运行,比如 Apache(例如 xampp)?
  • 我猜没有任何网络服务器在运行
  • 因为 PHP 是一种服务器端语言,据我所知,您需要运行某种 PHP 服务器才能使用 PHP 进行任何操作。以 Apache 为例:apachefriends.org/index.html
  • 你确定在 localhost 上调用了正确的 url 吗?
  • 我不知道,因为我使用ajax发布到php

标签: javascript php jquery google-chrome


【解决方案1】:

你应该试试

echo json_encode($total);

而不是

echo $total;

【讨论】:

  • echo 会显示结果,因此 js 中的数据变量将具有相同的值.. @foobar
【解决方案2】:

可以试试:

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);

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 2015-06-06
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多