Javascript Ajax PHP Jquery - 回显数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8265699/
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
Ajax PHP Jquery - echo-ing back data
提问by Howdy_McGee
I'm having trouble bringing back data from my PHP file. I guess I don't really understand the data parameter of this jquery function so I just went off some tutorials.
我在从 PHP 文件中取回数据时遇到问题。我想我不太了解这个 jquery 函数的数据参数,所以我只是离开了一些教程。
Jquery
查询
$.ajax(
{
url: 'test.php',
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
Now from my understanding the test:
declares the variable used in php and 1
is the value in that variable. But I'm not entirely sure...
现在根据我的理解,test:
声明了 php 中使用的变量,并且1
是该变量中的值。但我不完全确定......
Here's my PHP
这是我的 PHP
$item1 = $_POST['test'];
echo $item1;
Now it's just supposed to alert that value so I know it is at least returning something but in the alert it's just blank so I'm losing the value somewhere, but where?
现在它只是应该提醒该值,所以我知道它至少返回了一些东西,但在警报中它只是空白,所以我在某处丢失了值,但是在哪里?
回答by Rafay
use $_REQUEST
it will handle both the GET
and POST
使用$_REQUEST
它将处理GET
和POST
$item1 = $_REQUEST['test'];
by default ajax request is GET
type, either specify expilicitly the type
like
默认情况下ajax请求是GET
类型,或者明确指定之type
类的
$.ajax(
{
url: 'test.php',
type:'POST'
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
or use $_GET
like
或使用$_GET
像
item1 = $_GET['test'];
echo $item1;
回答by T.Todua
The correct way:
正确的方法:
<?php
$change = array('key1' => 'blabla', 'key2' => '12432rr424234');
echo json_encode($change);
?>
Then the jquery script:
然后是jquery脚本:
<script>
$.get("location.php", function(data){
var mydata= $.parseJSON(data);
var art1 = mydata.key1; // <----------- access the element
});
</script>
回答by Dinesh
This is work for me
这对我来说是工作
ajax code
阿贾克斯代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<title>Test Page</title>
<script>
$.ajax(
{
url: 'test.php',
type: 'POST',
dataType: 'text',
data: {latitude: '7.15588546', longitude: '81.5659984458'},
success: function (response)
{
alert(response);
}
});
</script>
</head>
<body>
</body>
</html>
php code (test.php)
php 代码 (test.php)
<?php
$lat = $_REQUEST['latitude'];
$lon = $_REQUEST['longitude'];
echo 'latitude- '.$lat . ', longitude- ' . $lon;
?>
回答by Axel
Been struggling on this for a while, my issue wasn't on the js but in php script : error reporting was set to E_ALL - which apparently blocks it from returning data to the AJAX call. My guess is it prints notices before the final echo and blocks it all. Weird thing is it didn't print me any notice in the logs i set up to get to the bottom of this.
在这方面苦苦挣扎了一段时间,我的问题不是在 js 上,而是在 php 脚本中:错误报告设置为 E_ALL - 这显然阻止了它向 AJAX 调用返回数据。我的猜测是它在最终回声之前打印通知并将其全部阻塞。奇怪的是,它没有在我设置的日志中打印任何通知以深入了解这一点。
Changed it to E_ERROR and now works like a charm.
将其更改为 E_ERROR,现在就像一个魅力。
回答by Sarangarajan V Iyengar
If you are trying to get information from the php file, I don't think the data field is required. Here is what I have.
如果您试图从 php 文件中获取信息,我认为不需要数据字段。这是我所拥有的。
$.ajax(
{
url: 'response.php',
type: 'get',
dataType:'text',
success: function(data){
window.alert(data);
}
}
and as far as the php goes..
就php而言..
<?php
echo "1";
echo "2";
echo "3";
echo "4";
echo "5";
echo "6";
echo "7";
echo "8";
?>
回答by Alex Pliutau
add POST method:
添加POST方法:
$.ajax(
{
url: 'test.php',
dataType: 'text',
type: 'post',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
回答by Punit
you forgot to put the method POST/GET by which you are sending the data to your php file.
您忘记将用于将数据发送到 php 文件的 POST/GET 方法。
$.ajax(
{
type:'POST',
url: 'test.php',
dataType: 'text',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})
回答by wargodz009
just add "type" POST or GET
//example
$.ajax(
{
url: 'test.php',
type: 'POST',
data: {test: '1'},
success: function(data)
{
window.alert(data);
}
})