阿贾克斯赢得了't调用PHP文件

Ajax won't call PHP file

本文关键字:调用 PHP 文件 阿贾克斯      更新时间:2023-09-26

我有一个Ajax函数,它应该调用searchengine.php,但什么也没发生
这是我的代码:

Ajax:

 $.ajax(){
     type: 'POST',
     url: 'search-engine.php',
     data: {userInput: searchInput},
     success: function(){
     alert('works');
     },
     error: function(){
     alert('something went wrong');
     }
    }

PHP:

<?php
$userInput = $_POST("userInput");
echo $userInput;
?>

我的输入在一个带有方法post的表单标记中。如果很重要。

您的Javascript都搞砸了。这就是它的外观(与您的代码进行比较和对比)。阅读文档!

 $.ajax('search-engine.php', {
      type: 'POST',
      data: { userInput:searchInput }
 }).done(function () {
      alert('works');
 }).fail(function () {
      alert('something went wrong');
 });

在PHP中,您使用的是()。您必须使用[]字符。如果你没有得到任何错误,那么你应该打开错误!

<?php
$userInput = $_POST["userInput"];
echo $userInput;

您的ajax调用是错误的。

您必须将config对象传递给ajax函数:

 $.ajax({
     type: 'POST',
     url: 'search-engine.php',
     data: {userInput: searchInput},
     success: function(){
         alert('works');
     },
     error: function(){
         alert('something went wrong');
     }
});