0

我的 jQuery ajax 发布不起作用。这是javascript

function SocialButtons() {  
var $buttonWrapper = jQuery('.WrapperDiv');
if ($buttonWrapper.length){
    var postData = $buttonWrapper.html();
    jQuery.ajax({
        type: 'POST',
        url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
        data: postData, 
        cache: false,
        success: function(data) { 
            console.log(data);
        },
        contentType: "application/json",
        dataType: 'json'    
    });
}
}

我正在保存要发布在隐藏 div 中的数据,例如

<div class='WrapperDiv hidden'>{"post_id":392,"url":"http:\/\/www.wordpress-site\/post\/post-title\/","title":"SEO Friendly title"}</div>

我从 post.php 页面得到的只是一个空数组。这是我的 post.php 代码

<?php
if(isset($_POST)){
    print_r($_POST);
} else {
    echo "0";
}   
?>

有什么想法吗?

编辑:我删除后它的工作

    contentType: "application/json",
    dataType: 'json' 
4

4 回答 4

0

有几件事要尝试,

尝试先将数据直接传递给数据对象。如果它有效,那么您可以调试并查看为什么它没有准备好您的隐藏 div。

而不是 $buttonWrapper.html 尝试 $buttonWrapper.text();

function SocialButtons() {  
var $buttonWrapper = jQuery('.WrapperDiv');
if ($buttonWrapper.length){
    var postData = $buttonWrapper.**text**();
    jQuery.ajax({
        type: 'POST',
        url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
        data: **{'id':1}**, 
        cache: false,
        success: function(data) { 
            console.log(data);
        },
        contentType: "application/json",
        dataType: 'json'    
    });
}
}
于 2013-04-26T17:29:19.573 回答
0

像这样的东西怎么样:

var postData = "data=" + encodeURCIComponent($buttonWrapper.html());

比在 PHP 中:

echo $_POST["data"];

比解析它什么的......

于 2013-04-26T17:25:36.653 回答
0

我删除后它的工作

contentType: "application/json",
dataType: 'json' 
于 2013-04-29T09:50:01.023 回答
0

在您的 jQuery ajax 调用中,您的数据未设置为 $_POST 变量名称。因此为什么什么都没有显示

尝试将您的功能更改为:

function SocialButtons() {  
  var buttonWrapper = jQuery('.WrapperDiv');
  if (buttonWrapper.length){
    var postData = buttonWrapper.html();
    jQuery.ajax({
    type: 'POST',
    url: 'http://www.wordpress-site.com/wp-contents/themes/theme-name/post.php',
    data: {postData: postData}, 
    cache: false,
    success: function(data) { 
        console.log(data);
    },
    contentType: "application/json",
    dataType: 'json'    
    });
  }
}

那么你应该在你的or上有一个$_POST['postData']变量。print_rvar_dump$_POST

于 2013-04-26T17:33:47.780 回答