Javascript 如何从 JQuery Ajax 请求中获取“数据”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27700513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get "data" from JQuery Ajax requests
提问by Daniyaal Khan
this is the code that I have on index.html:
这是我在 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>
How do I program test.php to get the "data" that is sent in the AJAX API?
如何编写 test.php 以获取在 AJAX API 中发送的“数据”?
采纳答案by nitigyan
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 教程。只是为了帮助您一点(假设您知道发送数据的 GET 和 POST 方法),数据中的“数据”:“检查”与功能(数据)中的“数据”不同。为清楚起见,您应该将它们命名为与此处不同的名称:
$.ajax({
type: "POST",
url: 'test.php',
data: "check",
success: function(response){
alert(response);
}
});
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:
这清楚地表明,一个是您在 POST 参数中发送到 test.php 文件的数据,另一个是您从 test.php 文件运行后获得的响应。实际上,您 POST 到 test.php 的数据参数必须是像这里这样的散列(我在这里假设键为“类型”:
$.ajax({
type: "POST",
url: 'test.php',
data: {"type":"check"},
success: function(response){
alert(response);
}
});
There can obviously be more key-val pairs in data.
数据中显然可以有更多的键值对。
So, assuming your test.php file is something like this:
所以,假设你的 test.php 文件是这样的:
if(isset($_POST['type'])){
//Do something
echo "The type you posted is ".$_POST['type'];
}
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.
在这种情况下,您的警报应显示为:“您发布的类型是支票”。这将根据您在 AJAX 调用中为“类型”键发送的值而变化。
回答by Deepu
You can try like this
你可以这样试试
$.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
}
});
And in test.php
在 test.php 中
if(isset($_POST['data']) && $_POST['data'] == 'check'){
//$_POST['data'] contain the value that you sent via ajax
//Do something
echo 'Success';
}
You can check thisfor more
您可以 查看更多
回答by Hoja.M.A
If you want to deals with more datas with jquery ajax. I prefer json data type.
如果你想用 jquery ajax 处理更多的数据。我更喜欢 json 数据类型。
Simply Use like this.
像这样简单地使用。
$.ajax({
type: "POST",
url: 'test.php',
dataType: 'json',
data: {"data":"check"},
success: function(data){
alert(data.value1);
alert(data.value2);
}
});
In your PHP Code
在您的 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;
}
回答by Venkata Krishna
Your HTML would be
你的 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>
Notice that data is an array, so you will have to pass a variable and its value in data. You can pass multiple variables in data, separating them with comma.
请注意,data 是一个数组,因此您必须在 data 中传递一个变量及其值。您可以在数据中传递多个变量,用逗号分隔它们。
And to pick the data sent by your ajax, in test.php:
并在 test.php 中选择 ajax 发送的数据:
<?php
if(isset($_POST['myvar']))
{
$myVariable = $_POST['myvar'];
echo $myVariable; //This would output the string passed in ajax, check
}
?>
$_POST is dependent on the type used in the AJAX call. If type is GET, in php it would be $_GET. A simple thing instead would be $_REQUEST, which irrespective of whether the AJAX call is of type GET or POST.
$_POST 取决于 AJAX 调用中使用的类型。如果类型是 GET,在 php 中它将是 $_GET。一个简单的事情是 $_REQUEST,这与 AJAX 调用是 GET 类型还是 POST 类型无关。
回答by Priyank
$.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.');
}
}
});
In test.php
在 test.php 中
<?php
if(isset($_POST['data'])){
echo 'successful';
}
?>
回答by user3111011
Output of the PHP file will sent to your AJAX succes function.
PHP 文件的输出将发送到您的 AJAX 成功函数。

