我发送的信息:
switch = rssAdd& data = [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[ object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[ object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[ object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[ object object],[object Object],[object Object],[object Object],[object Object]
由此创建:
$(xml).find('item').each(function() {
var stage = $(this).attr('p4:stage');
var title = $(this).find('title').text();
var desc = $(this).find('description').text();
var location = $(this).find('category').eq(0).text();
var billtype = $(this).find('category').eq(1).text();
var linkurl = $(this).find('link').text();
var thedate = $(this).find('a10\\:updated,updated').text();
thedate = thedate.substring(0,10);
var todaydate = $.datepicker.formatDate('yyyy-mm-dd', new Date());
//if(todaydate == thedate) {
info.push({'stage': stage,'title': title,'description': desc,'location': location,'billtype': billtype,'linkurl': linkurl,'thedate': thedate});
//}
});
console.log(info);
var senddata = json_encode(info);
$.ajax({
url: 'php.php',
type: 'POST',
dataType: 'text',
data: 'switch=rssAdd&data='+senddata,
success: function(result) {
//console.log('here is the result: ' + result);
}
});
它收到了一个php.php文件:
<?php
header("Content-Type: application/json");
include('sql.php');
$switch = $_POST['switch'];
switch ($switch) {
case "rssAdd":
$info = json_decode($_POST['data']);
print_r($info);
// $sql = "INSERT INTO rssFeed (date, title, description, location, billtype, link) VALUES(". $t['date'] .",". $t['title'] .", ". $t['description'] .", ". $t['location'] .", ". $t['billtype'] .", ". $t['link'] .")";
// $inres = $mysqli->query($sql);
// echo "worked: ".$t['title'];
break;
}
?>
由于某些原因,我无法弄明白,我认为是因为我不确定我发送给php的对象是什么类型,即使我已经尝试了所有,为什么它不被PHP和`print_r($ info)是空的,没有错误?
有什么想法吗?
先谢谢
安德鲁
答案 0 :(得分:1)
我认为javascript中的json_encode存在问题,因为它的结果是一系列[object Object]。它出了故障。
通常使用JSON.stringify来对json对象进行字符串化。