如何从 JQuery Ajax 请求中获取“数据”

How to get "data" from JQuery Ajax requests

本文关键字:获取 数据 请求 JQuery Ajax      更新时间:2023-09-26

这是我在index上的代码.html:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script>
            $.ajax({
                type: "POST",
                url: 'test.php',
                data: "check",
                success: function(data){
                    alert(data);
                }
            });
        </script>
    </head>
    <body>
        <div></div>
    </body>
</html>

如何对测试进行编程.php以获取在 AJAX API 中发送的"数据"?

你在这里问一个非常基本的问题。您应该首先浏览一些 Ajax 教程。只是为了帮助你一点(假设你知道发送数据的GET和POST方法),数据中的"数据":"检查"与函数中的"数据"(数据)是不同的。为清楚起见,您应该将它们命名为不同的名称,如下所示:

$.ajax({
     type: "POST",
     url: 'test.php',
     data: "check",
     success: function(response){
         alert(response);
     }
});

这清楚地表明,一个是你发送到测试的数据.php POST 参数中的文件,另一个是你在测试.php文件运行后从它获得的响应。事实上,你 POST 测试的数据参数.php必须是像这里这样的哈希值(我假设这里的键是"类型":

$.ajax({
     type: "POST",
     url: 'test.php',
     data: {"type":"check"},
     success: function(response){
         alert(response);
     }
});

数据中显然可以有更多的键值对。

因此,假设您的 test.php 文件如下所示:

if(isset($_POST['type'])){
  //Do something
  echo "The type you posted is ".$_POST['type'];
}

在这种情况下,您的警报应显示为:"您发布的类型是检查"。这将根据您在 AJAX 调用中为"类型"键发送的值而变化。

你可以这样尝试

 $.ajax({
    type: "POST",
    url: 'test.php',
    data: {"data":"check"},
    success: function(data){
        alert(data);//This will alert Success which is sent as the response to the ajax from the server
    }
 });

并在测试中.php

if(isset($_POST['data']) && $_POST['data'] == 'check'){
  //$_POST['data'] contain the value that you sent via ajax
  //Do something
  echo 'Success';
}

您可以查看此内容以获取更多信息

如果你想用jquery ajax处理更多的数据。我更喜欢 json 数据类型。

只需像这样使用即可。

$.ajax({
    type: "POST",
    url: 'test.php',
    dataType: 'json',
    data: {"data":"check"},
    success: function(data){
        alert(data.value1);
        alert(data.value2);
    }
 });

在你的 PHP 代码中

if(isset($_POST['data']) && $_POST['data'] == 'check'){
   //Data 1
     $data['value1'] = 'Data 1 Got Successfully';
    //Data 2
     $data['value2'] = 'Data 2 Got Successfully';
     $resonse = json_encode($data);
     echo $response;
}

你的HTML将是

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script>
     $.ajax({
     type: "POST",
     url: 'test.php',
     data: {"myvar":"check",},
     success: function(data){
         alert(data);
     }
     });
 </script>
</head>
<body>
<div></div>
</body>
</html>

请注意,数据是一个数组,因此您必须在数据中传递一个变量及其值。您可以在数据中传递多个变量,并用逗号分隔它们。

在测试中选取 ajax 发送的数据.php:

<?php
if(isset($_POST['myvar']))
{
  $myVariable = $_POST['myvar'];
  echo $myVariable;   //This would output the string passed in ajax, check
}
?>

$_POST 取决于 AJAX 调用中使用的类型。如果类型是 GET,在 php 中它将是 $_GET。一个简单的事情是 $_REQUEST,无论 AJAX 调用的类型是 GET 还是 POST。

$.ajax({//create an ajax request to load_page.php
    type: "POST",
    url: "test.php",
    data:{"data":"check"},
    success: function(data) {
        if (data) {
           alert(data);
        }
        else {
            alert('Successfully not posted.');
        }
    }
});

测试中.php

<?php 
if(isset($_POST['data'])){
    echo 'successful';
}
?>

PHP 文件的输出将发送到您的 AJAX 成功函数。