php 将ajax数据发布到php然后获取数据不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18700204/
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
Post ajax data to php and then get the data not work
提问by Newbie123
i've got some problem
我有一些问题
my html: http://jsfiddle.net/dHdnb/
我的 html:http: //jsfiddle.net/dHdnb/
my jquery:
我的jQuery:
$(".header_nav li a").click(function(){
var href = this.href;
$.ajax({
url: 'dynamic.php',
type: 'POST',
data: { target: href },
success: setTimeout(function(){
$.ajax({
url: 'dynamic.php',
dataType: 'html',
data: { target: href},
success: function(data) {
$(".container").html(data)
}
})
}, 1000)
})
here is my php code:
这是我的 php 代码:
<?php
$target = $_POST["target"];
echo $target;
function home(){
echo $target;
// some command
}
switch($target) {
case "home": home();
break;
// and so on
default;
}
$target = isset($_POST['target']) ? $_POST['target'] : 'default_target_value';
echo $target;
echo "Test ajax";
?>
lets me explain this, if the user click the button on those list
then, it will post the target variable into the server
then, the server will process the request and launch a function
finally, when the ajax process success, it will load the data from the server into the container div
让我解释一下,如果用户单击这些列表上的按钮
,它会将目标变量发布到服务器中,
然后服务器将处理请求并
最终启动一个函数,当 ajax 处理成功时,它将加载数据从服务器到容器div
my question is, why it's gave me an error like this?
"Notice: Undefined index: target in xxx.php on line 7"
i know there must be something wrong with my data on my ajax,
but i don't know where's my mistakes
please help me :)
我的问题是,为什么它给了我这样的错误?
“注意:未定义索引:第 7 行 xxx.php 中的目标”
我知道我的 ajax 上的数据肯定有问题,
但我不知道我的错误在哪里,
请帮助我:)
when i'm debug it with charles, the ajax send the data with text string like this
my POSTrequest raw:
当我用 charles 调试它时,ajax 发送带有文本字符串的数据,就像
我的POST请求原始:
POST /xxx/dynamic.php HTTP/1.1
Host xxx
Content-Length 67
Accept /
Origin http ://xxx
X-Requested-With XMLHttpRequest
User-Agent xx Content-Type application/x-www-form-urlencoded; charset=UTF-8
Referer http:/xxx.php
Accept-Encoding gzip,deflate,sdch
Accept-Language en-US,en;q=0.8target=http%3A%2F%2Fxx%2Fxx%2Fhome.php%23product
POST /xxx/dynamic.php HTTP/1.1
Host xxx
Content-Length 67
Accept /
Origin http://xxx
X-Requested-With XMLHttpRequest
User-Agent xx Content-Type application/x-www-form-urlencoded; charset=UTF-8
Referer http://xxx.php
Accept-Encoding gzip,deflate,sdch
Accept-Language en-US,en;q=0.8目标=http%3A%2F%2Fxx%2Fxx%2Fhome.php%23product
my POSTresponse raw:
我的POST响应原始:
HTTP/1.1 200 OK
Date: Tue, 10 Sep 2013 09:35:19 GMT
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 57
Content-Type: text/htmlhttp ://xxx.php#productTest ajax
HTTP/1.1 200 OK
日期:2013 年 9 月 10 日星期二 09:35:19 GMT
服务器:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By:PHP/5.4.16
内容-长度:57
内容类型:文本/htmlhttp://xxx.php#productTest ajax
my GETrequest raw:
我的GET请求原始:
GET xxx.php HTTP/1.1
Host: xxx
Accept: text/html, /; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: xx
Referer: http ://xxx.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
GET xxx.php HTTP/1.1
Host: xxx
Accept: text/html, /; q=0.01
X-Requested-With: XMLHttpRequest
User-Agent: xx
Referer: http://xxx.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
my GETresponse raw:
我的GET响应原始:
HTTP/1.1 200 OK
Date: Tue, 10 Sep 2013 09:45:43 GMT
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 152
Content-Type: text/htmlNotice: Undefined index: target in dynamic.phpon line 2
Test ajax
HTTP/1.1 200 OK
日期:2013 年 9 月 10 日星期二 09:45:43 GMT
服务器:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By:PHP/5.4.16
内容-长度:152
内容类型:文本/html注意:Undefined index: target in dynamic.phpon line 2
Test ajax
回答by Newbie123
There is the valid code!
有有效代码!
var href = this.href;
$.ajax({
url: 'dynamic.php',
type: 'POST',
data: { target: href },
success: function (data) {
setTimeout(function (){
$(".container").html(data)
}, 1000)
}
});
you don't need to do the get ajaxcommand, you just need to directly output the data in the success:above! :)
不需要做get ajax命令,只需要在success:上面直接输出数据即可!:)
回答by Quentin
You aren't encoding the data in a format that PHP can decode and populate $_POST
with. You are sending a plain text string.
您没有以 PHP 可以解码和填充的格式对数据进行编码$_POST
。您正在发送纯文本字符串。
Change:
改变:
data: target,
to
到
data: { target: this.href },
回答by Marc B
When sending data via AJAX like you are, you have to replicate a form submission, e.g.
像您一样通过 AJAX 发送数据时,您必须复制表单提交,例如
$.ajax(...
data: 'foo'
);
will send a bare string 'foo' over to the server. But having
将向服务器发送一个裸字符串 'foo'。但是有
$.ajax(...
data: 'bar=foo'; // key=value pair
// OR
data: {bar: 'foo'} // javascript key/value object
);
you'll get the proper data for PHP to populate $_GET/$_POST for you. No key/value pair, no entry in $_GET/$_POST. It's that simple.
您将获得 PHP 为您填充 $_GET/$_POST 的正确数据。没有键/值对,$_GET/$_POST 中没有条目。就这么简单。
回答by Gadoma
Mate in your ajax definition shouldnt you pass the data as array? try changing the data: line with the below, should work for you:
在您的 ajax 定义中,您不应该将数据作为数组传递吗?尝试更改数据:符合以下内容,应该适合您:
data: { target: the_varibable_you_want_to_pass_by_post }
EDIT: as per your comment and edited OP source and @Arthur answer
编辑:根据您的评论和编辑的 OP 源和@Arthur 回答
$(".header_nav li a").click(function(){
var clicked_link = $(this); //the variable stores the refference to the clicked "a" element
$.ajax({
url: 'dynamic.php',
type: 'POST',
data: { target: clicked_link.href }, //so now you can reffer to the href of the "a"
success: $.ajax({
url: 'dynamic.php',
type: 'POST',
data: { target: clicked_link.href },
dataType: 'html',
success: function(data){
$(".container").html(data)
}
})
})
In your previous code, the "this" did not reffer to the clicked link but to the ajax object that does not have the 'href' attribute.
在您之前的代码中,“this”并不指向单击的链接,而是指向没有“href”属性的 ajax 对象。
回答by Arthur
The problem is, that you don't send any data when making the secong request:
问题是,您在发出 secong 请求时不发送任何数据:
$(".header_nav li a").click(function(){
$.ajax({
url: 'dynamic.php',
type: 'POST',
data: { target: this.href }, //i can see the data here
success: $.ajax({
url: 'dynamic.php', //notice the same PHP script
dataType: 'html', // where is the data in this request?
success: setTimeout(function(data){
$(".container").html(data)
}), 1000)
})
});
So at first it works, but when performing a second request, PHP can't find the 'target' because you are not sending it.
所以一开始它可以工作,但是在执行第二个请求时,PHP 找不到“目标”,因为您没有发送它。
And it's better to cache the href first:
最好先缓存 href :
$(".header_nav li a").click(function() {
var href = this.href;
$.ajax({
url: 'dynamic.php',
type: 'POST',
data: {
target: href //data for first request
},
success: $.ajax({
url: 'dynamic.php',
dataType: 'html',
data: {
target: href //data for the second request
},
success: setTimeout(function(data) {
$(".container").html(data)
}),
1000);
})
});
});
Or another solution is to check if the "target" exists in PHP:
或者另一种解决方案是检查 PHP 中是否存在“目标”:
<?php
$target = isset($_POST['target']) ? $_POST['target'] : 'default_target_value';
function home(){
// some data like echo and so on
}
switch($target) {
case "home": home();
break;
// and so on
default;
}
echo "Test ajax";
?>