所以我使用这篇ajax帖子的目的是在wordpress循环中使用一些JS变量,这样我就可以根据ajax帖子中的变量调用自定义循环。
下面是ajax调用。
$('.collection-more').click(function() {
$.ajax({
type: 'post',
url: 'http://tmet.dev/wp-content/themes/muban-trust/single-collection.php',
data: { "test" : "hello" },
success: function( data ) {
console.log( data );
}
});
})目前我正在发送硬编码数据。
在我的单集合.php页面中:
<?php
print_r($_POST);
if(isset($POST['filters[Artist]']))
{
$filters_obj = $_POST['filters[Artist]'];
$filters_array = json_decode($filters_obj, TRUE);
for($i = 0; $i < sizeof($filters_array); $i++)
{
echo '<p>h'.$obj->name.'</p>';
}
}
?>我使用print_r只是为了调试这个问题,目前它返回:
Array()
问题是我不能访问单个集合.php文件中名为"test“的Json对象。
我如何访问它?
提前感谢!
编辑: firebug屏幕截图

发布于 2014-07-25 18:51:18
从ajax到php的 :这是传统的方式
var payload = {
smth1: "name",
smth2: "age"
};然后在调用ajax时
$.ajax({
url: "somephp.php",
type: 'POST',
data: payload,
dataType: 'json',
crossDomain: true
})从phpPost到javascript的:
获取post参数的正确方法:
$fields = $_POST['fields'];
$usernameGiven = $fields['smth1'];
$passwordGiven = $fields['smth2'];并在将smthn返回给javascript时
$result = array(
"something" => "something",
"something2" => $something2
);
echo json_encode($result);发布于 2014-07-25 19:02:34
使用$_POST['test']访问test参数。您的print_r()显示它已正确填写。
您的PHP代码正在访问$_POST['filters[Artist]'],但是在Javascript中没有这样的参数。如果你通过了:
data: { 'filters[Artist]': somevalue }你可以在PHP中以$_POST['filters']['Artist']的形式访问它。
https://stackoverflow.com/questions/24953991
复制相似问题