来自服务器的响应问题

时间:2016-02-16 10:18:03

标签: javascript php ajax

我用AJAX,PHP和JavaScript实现了一个计算器。计算在PHP代码中完成。当我向服务器发送响应请求时,我得到了PHP文件的完整源代码。我该如何防止这种情况?

这是我的JavaScript代码:

 var xmlHTTP;
    var operation;

    function process() {


        var number1 = document.getElementById("n1").value;

        var num1 = parseInt(number1);

        var number2 = document.getElementById("n2").value;
        var num2 = parseInt(number2);


        var answer;
        if (document.getElementById('+').checked) {
            operation='add';
        } else if (document.getElementById('-').checked) {
            operation='subs';
        }
        else if (document.getElementById('*').checked) {
            operation='mul';
        }
        else if (document.getElementById('/').checked) {
            operation='div';
        }

    }
    function loadDoc() {
        process();
        xmlHTTP=new XMLHttpRequest();
        xmlHTTP.onreadystatechange = function () {
            alert(xmlHTTP.readyState);
            alert(xmlHTTP.status);
            if (xmlHTTP.readyState == XMLHttpRequest.DONE) {

                document.getElementById("cal").i=xmlHTTP.responseText;
            }
        }
        xmlHTTP.open("POST","process.php?value1=num1&value2=num2&value3=operation", true);
        xmlHTTP.send();

    }


  <?php

$number1 = $_POST['value1'];
$number2=$_POST['value2'];
$answer=0;
$op=$_POST['operation'];

//echo $answer;
if($op="add"){
    $answer=$number1+$number2;
}
elseif($op="subs"){
    $answer=$number1-$number2;
}
elseif($op="mul"){
    $answer=$number1*$number2;
}
elseif($op="div"){
    $answer=$number1/$number2;
}
else{
    echo "Choose an operation";
}
echo $answer;

2 个答案:

答案 0 :(得分:1)

为了避免整个代码被发送回ajax函数,在执行caluclations的php脚本中尝试类似:

if( $_SERVER['REQUEST_METHOD']=='POST' ){
    ob_clean(); /* discard any output there might have been to this point */

    /* do the calculations */

    /* send response */
    echo $answer;

    exit();
}

延迟更新 现在看到你发布的附加代码我可以看到一些关于php的小错误 - 你使用单个=来测试一个值是否等于某个东西,这是不正确的,因为它用于设置值。您需要使用双==来测试相等性,或使用===进行严格的相等测试。 因此,结合我之前建议的内容,可能类似于此:

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        ob_clean();
        $number1 = $_POST['value1'];
        $number2 = $_POST['value2'];
        $answer=0;
        $op=$_POST['operation'];


        if($op=="add"){
            $answer=$number1+$number2;
        } elseif($op=="subs"){
            $answer=$number1-$number2;
        } elseif($op=="mul"){
            $answer=$number1*$number2;
        } elseif($op=="div"){
            $answer=$number1/$number2;
        } else{
            echo "Choose an operation";
        }
        if( $answer ) echo $answer;
        exit();
    }
?>

我仔细查看了您发布的javascript代码,发现了一些肯定无济于事的问题 - 希望以下内容可能有所帮助。

/* The values calculated inside this function would not necessarily have been available to 
   the ajax function so this function returns them as an object. */

function process() {
    var operation;
    var number1 = document.getElementById("n1").value;
    var num1 = parseInt(number1);

    var number2 = document.getElementById("n2").value;
    var num2 = parseInt(number2);

    var answer;
    /* Are these valid IDs for DOM elements??? */
    if( document.getElementById('+').checked ) operation='add';
    else if( document.getElementById('-').checked ) operation='subs';
    else if( document.getElementById('*').checked ) operation='mul';
    else if( document.getElementById('/').checked ) operation='div';

    return {
        n1:num1,
        n2:num2,
        op:operation    
    }
}

/* You never actually sent the values from the `process` function as they
   were not properly escaped/unquoted. */

function loadDoc() {
    var obj=process();
    var xmlHTTP=new XMLHttpRequest();
    xmlHTTP.onreadystatechange = function () {
        if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200 ) {
            document.getElementById("cal").innerHTML=xmlHTTP.responseText;
        }
    }
    xmlHTTP.open( "POST", "process.php?value1="+obj.n1+"&value2="+obj.n2+"&value3="+obj.op, true );
    xmlHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xmlHTTP.send();
}

答案 1 :(得分:0)

echo $answer;使用exit();die();后。 发送到页面的AJAX请求将返回整个页面源作为响应,如果脚本没有使用exit();停止,并且它们不应该是echo $answer;之前的任何其他回声,因为它也将被添加与你的回应。 您可以使用switch case而不是使用多个if和elseif语句。

例如:

...
Switch($op) {
    case 'add':
      $answer=$number1+$number2;
      break;
     ...
     ...

}


echo $answer;
die();