如何获得“数据”来自JQuery Ajax请求 [英] How to get "data" from JQuery Ajax requests

查看:155
本文介绍了如何获得“数据”来自JQuery Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

this is the code that I have on 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>

如何编写test.php来获取AJAX API中发送的数据?

How do I program test.php to get the "data" that is sent in the AJAX API?

推荐答案

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

You are asking a very basic question here. You should first go through some Ajax tutorials. Just to help you a little (assuming you are aware of GET and POST methods of sending data), 'data' in data: "check" is different than 'data' in function (data) are different. For clarity, you should name them different as here:

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

这清楚地表明一个是你在POST参数中发送到test.php文件的数据和其他是运行后从test.php文件中获得的响应。实际上,你POST到test.php的数据参数必须是像这里的哈希(我假设键在这里是类型:

This makes it clear that one is data that you are sending to the test.php file in POST parameters and other is the response you are getting from the test.php file after it is run. In fact, the data parameter that you POST to test.php has to be a hash like here (I am assuming the key as "type" here:

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

数据显然可以有更多的key-val对。

There can obviously be more key-val pairs in data.

所以,假设你的test.php文件是这样的:

So, assuming your test.php file is something like this:

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

在这种情况下,您的警报应显示为:您发布的类型是检查。这将根据您在AJAX呼叫中为类型键发送的值而更改。

In this case your alert should read: "The type you posted is check". This will change based on what value you send for 'type' key in AJAX call.

这篇关于如何获得“数据”来自JQuery Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆