php接收json 格式错误,javascript – 在php中以不完整的格式接收JSON数据?

我正在构建一个超级简单的网站应用程序,它调用API来检索用户输入到所述应用程序的任何网站的反向链接数据. API发送的数据包括字符串(例如

http://www.domain.com/?feed=rss)和数字,并且采用JSON格式.我解析响应如下:

mozResponse = JSON.parse(response);

然后我遍历这些数据,只将我想要的数据推送到2个新数组(arry,arry1),声明如下:

arry = [];

arry1 = [];

推动如下:

arry.push({id:i, url:mozResponse[i].uu, pa:Math.round(mozResponse[i].upa), da:Math.round(mozResponse[i].pda), anchor:mozResponse[i].lt});

然后我按如下方式对这两个数组进行Stringify:

var cautionArrayString = JSON.stringify(arry);

var dangerArrayString = JSON.stringify(arry1);

我正在使用JavaScript XMLHTTPRequest将此数据发布到php文件,如下所示:

var queryString = "email=" + Email + "&caution=" + cautionArrayString + "&danger=" + dangerArrayString;

xhr1.onreadystatechange=Response1;

xhr1.open("post","http://example.com/emails.php",true);

xhr1.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xhr1.send(queryString);

然后php文件读取:

$caution = $_POST['caution'];

$danger = $_POST['danger'];

我知道在这一点上我应该在最初这样做时再次解码JSON我最终得到了一个破碎的数组.以下是console.log在将数据发布到php文件后读取的内容,使用以下php文件:

echo($_POST['caution']);

echo ($_POST['danger']);

Console.log显示:

[{"id":3,"url":"example.ca/","pa":26,"da":12,"anchor”:”Example”},

{"id":4,"url":"example.ca/","pa":26,"da":12,"anchor":"thank you gifts"},

{"id":5,"url":"example.ca/","pa":26,"da":12,"anchor":"flowers"},

{"id":6,"url":"example.ca/","pa":26,"da":12,"anchor":"thank you"},

{"id":7,"url":"example.ca/","pa":26,"da":12,"anchor":"Arrive in Style"},

{"id":8,"url":"example.ca/","pa":26,"da":12,"anchor":"dignity"},

{"id":9,"url":"example.ca/","pa":26,"da":12,"anchor":"Beautiful in Blue"},

{"id":10,"url":"example.ca/","pa":26,"da":12,"anchor":"Blooming Garden Basket"},

{"id":11,"url":"example.ca/","pa":26,"da":12,"anchor":"Country Basket Blooms"},

{"id":12,"url":"example.ca/","pa":26,"da":12,"anchor":"Heart’s Delight"},

{"id":13,"url":"example.ca/","pa":26,"da":12,"anchor":"Make a Wish"},

{"id":14,"url":"example.ca/","pa":26,"da":12,"anchor":"Moondance"},

{"id":15,"url":"example.ca/","pa":26,"da":12,"anchor":"Queen’s Court"},

{"id":16,"url":"example.ca/","pa":26,"da":12,"anchor":"Sweet as Sugar"},

{"id":17,"url":"example.ca/","pa":26,"da":12,"anchor":"flower colors"},

{"id":18,"url":"example.ca/","pa":26,"da":12,"anchor":"Always Yours"},

{"id":19,"url":"example.ca/","pa":26,"da":12,"anchor":"Sunrise, Sunset"},

{"id":20,"url":"example.ca/","pa":26,"da":12,"anchor":"Uniquely Chic"},

{"id":21,"url":"example.com/best/index.php?page=1998","pa":25,"da":31,"anchor":"example.ca/"},

{"id":22,"url":"example.com/best/index.php?page=1994","pa":25,"da":31,"anchor":"example.ca/"},

{"id":23,"url":"example.ca/","pa":25,"da":16,"anchor”:”example”},

{"id":28,"url":"example.ca/article/156-best-cms-for-small-business","pa":22,"da":39,"anchor":"example.ca/"},

{"id":30,"url":"example.ca/blog.html","pa":21,"da":15,"anchor":"example.ca/"},

{"id":31,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor”:”Example”},

{"id":32,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Arrive in Style"},

{"id":33,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Beautiful in Blue"},

{"id":34,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Blooming Garden Basket"},

{"id":35,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Country Basket Blooms"},

{"id":36,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Heart’s Delight"},

{"id":37,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Make a Wish"},

{"id":38,"url":"example.ca/gifts.html","pa":19,"da":11,"anchor”:”example- Flowers"},

{"id":39,"url":"example.ca/category/flowers/","pa":19,"da":16,"anchor”:”Example”},

{"id":40,"url":"example.ca/category/floral-fauna/","pa":19,"da":16,"anchor”:”Example”},

{"id":41,"url":"nunavut.findstuffhere.ca/nunavut/?feed=rss2[]

你可以在哪里看到第一个数组是不完整的,第二个是空的(应该是!).所以我的问题是,造成这种情况的原因是什么?如何解决?

注意事项

>我使用3个URL作为测试输入.数据包含1个URL,但其他2个(包括上面的示例)返回这个不完整的JSON,似乎是因为查询字符串是从API返回的URL的一部分?

>我是console.log(mozResponse),输出是预期的

>我在console.log(arry)和console.log(arry1)之后迭代通过mozResponse并将数据从mozResponse推送到arry / arry1并且输出是一个完整的数组

>我是console.log(arry)和console.log(arry1)在应用JSON.stringify之后但是在发布到php并且输出完成之后

我探索过的事情

>我原本以为这是其中一个URL的结果

API返回包括查询字符串(例如?feed = rss2)

但是在此断点之前有查询字符串

处理得很好

>似乎不是我使用的JSON错误

json_last_error();它返回0.

>也似乎不是一个

JSON / POST char限制因为它在我返回破坏的JSON时

输入不同的URL(此URL的$_POST输出也是如此

在查询字符串后面断开,不确定这是否是巧合)

> Suhosin不存在

>创建了一个phpinfo页面来检查max_vars但是限制很大

任何帮助是极大的赞赏!

*不同的报价来自复制/粘贴!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值