我正在构建一个超级简单的网站应用程序,该应用程序调用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- <span class=\"catlist\"> Flowers</span>"},
{"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[]
你可以在那里看到第一个数组是不完整的,第二个是空的(应该是!)。所以我的问题是,导致这种情况的原因是什么?如何解决?
注意事项
console.log(mozResponse)
,输出符合预期console.log(arry) and console.log(arry1)
迭代通过mozResponse并将数据从mozResponse推送到arry / arry1并且输出是一个完整的数组console.log(arry)
和console.log(arry1)
应用JSON.stringify之后但是在发布到php并且输出完成之后我探索过的事情
非常感谢任何帮助!
*不同的引用来自复制/粘贴!
答案 0 :(得分:1)
您可能只需要编码您的值以便在查询字符串中使用:
var cautionArrayString = encodeURIComponent(JSON.stringify(arry));
var dangerArrayString = encodeURIComponent(JSON.stringify(arry1));
var queryString = "email=" + encodeURIComponent(Email) + "&caution=" + cautionArrayString + "&danger=" + dangerArrayString;
将其转换为json不会自动对其进行正确编码以在url中使用,因此值中的字符可能会破坏查询字符串。
假设不同的引号是由复制粘贴引起的......