gpt4 book ai didi

javascript - 无法使用ajax调用外部函数

转载 作者:行者123 更新时间:2023-12-02 15:49:24 25 4
gpt4 key购买 nike

这是我的 jquery 代码

<script>
$(function () {

$('#deleteform').on('submit', function (e) {

e.preventDefault();

$.ajax({
type: 'get',
url: 'delete.php',
success: function () {
alert('Worked');
}
});

});

});
</script>

还有我的 PHP 代码(我只是想测试一下,所以我添加了一个简单的函数)

<?php
header("Location: http://www.google.com/");
?>

当我单击按钮(表单提交时)时,除了“已工作”警报框之外,没有任何反应。但无论我在 PHP 文件 (delete.php) 中放入什么,都没有任何反应。我究竟做错了什么?我的“delete.php”文件将有一个脚本来删除 XML 文件中的数据,以防万一它更改了某些内容。 (现在我正在尝试使用一个简单的 php 行)

<小时/>

编辑

PHP 文件中真正的 PHP 代码是这样的:

<?php
$xml = simplexml_load_file("signatures.xml");
$name = $_POST['nom'];
$signs = $xml->xpath('//Signature[Nom = "'.$name.'"]');
$xml -> SignaturesParent -> removeChild($signs);
?>

当我尝试这样做时没有任何反应。

最佳答案

试试这个。

The ajax call now alerts whatever is sent to it from the delete.php

The ajax call does a POST and not a GET so that it matches the fact that you are using $_POST[''] and send some data i.e. smith you are going to have to change that to something that actually exists in your XML file

The delete.php actually returns something

The delete.php saves the changed xml document back to disk to a file with a different name, so you can see if it actually did anything. just while you are tesing.

<script>
$(function () {

$('#deleteform').on('submit', function (e) {

e.preventDefault();
$.ajax({
type: 'POST',
url: 'delete.php',
data: {nom:"smith"},
success: function (data) {
alert(data);
}
});
});
});
</script>



<?php
$xml = simplexml_load_file("signatures.xml");
$name = $_POST['nom'];
$signs = $xml->xpath('//Signature[Nom = "'.$name.'"]');
$xml -> SignaturesParent -> removeChild($signs);

$result = $xml->asXML("signatures2.xml");
echo $result ? 'File Saved' : 'File Not Saved';

?>

关于javascript - 无法使用ajax调用外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31949161/

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