您现在的位置是:首页 > 正文

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

2024-04-01 07:30:14阅读 1

我正在构建一个超级简单的网站应用程序,它调用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但是限制很大

任何帮助是极大的赞赏!

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

网站文章

  • Spring Cloud Alibaba笔记1

    Spring Cloud Alibaba笔记1

    Spring Cloud Alibaba笔记

    2024-04-01 07:29:42
  • vue生命周期的传统写法和setup语法糖写法

    vue生命周期的传统写法和setup语法糖写法

    在 Vue 3 中,由于引入了 Composition API,一些生命周期钩子函数的名称发生了改变,并且在使用 `` 语法糖时,不再直接使用这些钩子函数,而是使用Composition API 提供的函数来实现类似的功能。`beforeCreate` 和 `created` 这两个钩子函数在 Vue 3 中被封装进了 `` 语法糖中。原因是为了简化代码和提高可读性。

    2024-04-01 07:29:34
  • 单例设计模式Java实现的八种写法以及反序列化、反射的破坏与防御

    单例设计模式Java实现的八种写法以及反序列化、反射的破坏与防御

    目录单例设计模式概述主要步骤单例模式实现方案1. 饿汉式(静态常量,线程安全)优缺点说明2. 饿汉式(静态代码块,线程安全)优缺点说明3. 懒汉式(线程不安全)优缺点说明4. 懒汉式(同步方法,线程安...

    2024-04-01 07:29:26
  • CSP201909-1 小明种苹果

    CSP201909-1 小明种苹果

    import java.util.Scanner; public class Main{ public static void main (String[] args) { // System.out...

    2024-04-01 07:28:42
  • RocketMQ-高可用+扩展性

    名词约定 Region:物理区域,例如不同的机房 Broker-MS:broker最基本的高可用master-slave结构,包含1台master+1台slave Broker-Cluster:由多个...

    2024-04-01 07:28:33
  • 计算机四级理论知识试卷答案,计算机系统操作员四级理论知识试卷(含答案).doc...

    计算机系统操作员四级理论知识试卷(含答案)职业技能鉴定国家题库计算机操作员四级理论知识试卷单项选择题(第1题~第160题。选择一个正确的答案,将相应的字母填入题内的括号中。每题0.5分,共80分。)正...

    2024-04-01 07:28:25
  • 计算机管理术语路径描述的是,directory

    directory(计算机语言术语)语音编辑锁定讨论上传视频directory,英文单词。意思是n.人名地址录,(电话)号码簿;(计算机文件或程序的)目录。adj.指导的,指挥的;咨询的;管理的。di...

    2024-04-01 07:27:37
  • 【C++】c++入门

    【C++】c++入门 1.c++的诞生 2.c++和c的关系 3.c++的开发过程 4.c++的特点 C++是一种面向对象的计算机程序设计语言,由美国的本贾尼·斯特劳斯特卢普博士在20世纪80年代初期发明并实现。C++是C语言的继承,进一步扩充和完善了C语言,成为一种面向对象的程序设计语言。 它是一种静态数据类型检查的、支持多重编程范式的通用程...

    2024-04-01 07:27:30
  • 单元测试快速自定义生成---开发者的福音

    单元测试快速自定义生成---开发者的福音

    作为一个开发者,越发觉得单元测试是必须的,至于单元测试是由开发编写还是测试编写,我觉得还得看公司的技术氛围。有一个好的编写单元测试习惯的开发者,代码质量肯定是很好的,可以随时校验自己开发和改写接口的快速检查工具。也避免了测试提的bug多而影响个人绩效(有些公司把bug计入考核范围内)。而作为开发者又不想过多花费时间在单元测试中(毕竟一般开发很忙的),所以本文提供了一种快速自定义的生成...

    2024-04-01 07:27:23
  • jquery 和 js 的区别

     1、加载DOM区别 JavaScript: window.onload function first(){ alert('first'); } function second(){ alert('second'); } window.onload = first; window.onload = second; //只会执行第二个wind

    2024-04-01 07:26:42