The Ajax function below sends data from a page to the same page where it is interpreted by PHP.
下面的Ajax函数将数据从页面发送到PHP解释的同一页面。
Using Firebug we can see that the data is sent, however it is not received by the PHP page. If we change it to a $.get function and $_GET the data in PHP then it works.
使用Firebug,我们可以看到数据已发送,但PHP页面未收到。如果我们将它更改为$ .get函数并使用PHP _GET数据然后它可以工作。
Why does it not work with $.post and $_POST
为什么它不适用于$ .post和$ _POST
$.ajax({
type: "POST",
url: 'http://www.example.com/page-in-question',
data: obj,
success: function(data){ alert(data)},
dataType: 'json'
});
4 个解决方案
#1
0
if there is a problem, it probably in your php page. Try to browse the php page directly in the browser and check what is your output. If you need some inputs from post just change it to the GET in order to debug
如果有问题,可能在你的php页面中。尝试直接在浏览器中浏览php页面并检查输出结果。如果您需要来自post的一些输入,只需将其更改为GET即可进行调试
#2
0
try this
var sname = $("#sname").val();
var lname = $("#lname").val();
var html = $.ajax({
type: "POST",
url: "ajax.class.php",
data: "sname=" + sname +"&lname="+ lname ,
async: false
}).responseText;
if(html)
{
alert(html);
return false;
}
else
{
alert(html);
return true;
}
alax.class.php
<php
echo $_REQUEST['sname'];
echo $_REQUEST['sname'];
?>
#3
0
Ajax on same page will not work to show data via POST
etc because, PHP has already run that's why you would typically use the external page to process your data and then use ajax to grab the response.
同一页面上的Ajax无法通过POST等显示数据,因为PHP已经运行了这就是为什么你通常会使用外部页面来处理你的数据,然后使用ajax来获取响应。
example
success: function(){
$('#responseDiv').text(data);
}
#4
0
You are posting the data... Check if the target is returning some data or not. if it returns some data then only you can see the data otherwise not.
您正在发布数据...检查目标是否正在返回某些数据。如果它返回一些数据,那么只有你才能看到数据。
add both success and error.. so that you can get what exactly
添加成功和错误..这样你就可以得到什么
success: function( data,textStatus,jqXHR ){
console.log(data);//if it returns any data
console.log(textStatus);//or alert(textStatus);
}
error: function( jqXHR,textStatus,errorThrown ){
console.log("There is some error");
console.log(errorThrown);
}
#1
0
if there is a problem, it probably in your php page. Try to browse the php page directly in the browser and check what is your output. If you need some inputs from post just change it to the GET in order to debug
如果有问题,可能在你的php页面中。尝试直接在浏览器中浏览php页面并检查输出结果。如果您需要来自post的一些输入,只需将其更改为GET即可进行调试
#2
0
try this
var sname = $("#sname").val();
var lname = $("#lname").val();
var html = $.ajax({
type: "POST",
url: "ajax.class.php",
data: "sname=" + sname +"&lname="+ lname ,
async: false
}).responseText;
if(html)
{
alert(html);
return false;
}
else
{
alert(html);
return true;
}
alax.class.php
<php
echo $_REQUEST['sname'];
echo $_REQUEST['sname'];
?>
#3
0
Ajax on same page will not work to show data via POST
etc because, PHP has already run that's why you would typically use the external page to process your data and then use ajax to grab the response.
同一页面上的Ajax无法通过POST等显示数据,因为PHP已经运行了这就是为什么你通常会使用外部页面来处理你的数据,然后使用ajax来获取响应。
example
success: function(){
$('#responseDiv').text(data);
}
#4
0
You are posting the data... Check if the target is returning some data or not. if it returns some data then only you can see the data otherwise not.
您正在发布数据...检查目标是否正在返回某些数据。如果它返回一些数据,那么只有你才能看到数据。
add both success and error.. so that you can get what exactly
添加成功和错误..这样你就可以得到什么
success: function( data,textStatus,jqXHR ){
console.log(data);//if it returns any data
console.log(textStatus);//or alert(textStatus);
}
error: function( jqXHR,textStatus,errorThrown ){
console.log("There is some error");
console.log(errorThrown);
}