gpt4 book ai didi

javascript - 服务器响应问题

转载 作者:行者123 更新时间:2023-12-03 07:40:28 26 4
gpt4 key购买 nike

我用 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;

最佳答案

为了避免整个代码被发送回 ajax 函数,在执行计算的 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();
}

关于javascript - 服务器响应问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35429734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com