gpt4 book ai didi

javascript - ajax请求成功,但是php没有运行

转载 作者:行者123 更新时间:2023-11-28 11:21:03 24 4
gpt4 key购买 nike

我有一个非常简单的 jquery 函数,它将 Ajax 调用发送到 php 文件,该文件应该回显警报,但对于我来说,无法让它运行。现在,我只是想触发 php 运行。这是 JavaScript:

function getObdDescription(){

var $code = document.getElementById("vehicle_obd_code").value;
var $length = $code.length;


if($length == 5){
window.confirm($length);

$.ajax({ url: '/new.php',
data: {action: 'test'},
type: 'post',
success:function(result)//we got the response
{
alert('Successfully called');
},
error:function(exception){alert('Exception:'+exception);}
});
}
return false;
}

这里是new.php

<?php
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>';
?>

我正在 Chrome 中进行测试,打开网络选项卡,可以看到调用成功,并且我收到弹出的“调用成功”消息,因此 jquery 正在运行,并且 Ajax调用成功。我还知道 url: '/new.php 是正确的,因为当我从服务器中删除 new.php 时,我从控制台和网络选项卡中收到状态“404(未找到)” 。我什至在没有条件 if($length ==... 的情况下进行了测试,但仍然没有运气。当然,我知道这不是问题,因为我得到了“成功调用”响应。有什么想法吗?

最佳答案

如果您需要提醒文本,这不是它的工作方式,您应该在ajax success函数的前端执行此操作,遵循 KISS(保持简单愚蠢)并在 php 中仅回显文本。这是正确的做法。

你应该这样做:

function getObdDescription() {

var $code = document.getElementById("vehicle_obd_code").value;
var $length = $code.length;


if ($length == 5) {
window.confirm($length);

$.ajax({
url: '/new.php',
data: {
action: 'test'
},
type: 'post',
success: function (result) //we got the response
{
alert(result);
},
error: function (exception) {
alert('Exception:' + exception);
}
});
}
return false;
}

在你的 PHP 中

<?php
echo 'message successfully sent';
?>

关于javascript - ajax请求成功,但是php没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514300/

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