我的jQuery ajax發佈不起作用。這裏是我保存數據的JavaScriptjQuery Ajax發佈不起作用
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'
的HTML抓住也只是一個字符串後的工作?你需要從中創建一個真正的數組。 – PlantTheIdea 2013-04-26 17:20:49
postData只是一個字符串,而不是一個對象。使用字符串時,字符串必須作爲查詢字符串構建,而不是JSON。 – adeneo 2013-04-26 17:21:04
@adeneo:任何小提琴? – 2013-04-26 17:42:02