I'm building a super simple website application that calls an API to retrieve backlink data for any website inputted into said application by the user. The data the API sends includes strings (e.g. http://www.domain.com/?feed=rss) and numbers and is in JSON format. I parse the response as follows:
mozResponse = JSON.parse(response);
I then iterate through this data, pushing only the data I want into 2 new arrays (arry, arry1), declared as follows:
arry = [];
arry1 = [];
Pushing as follows:
arry.push({id:i, url:mozResponse[i].uu, pa:Math.round(mozResponse[i].upa), da:Math.round(mozResponse[i].pda), anchor:mozResponse[i].lt});
I then Stringify these two arrays as follows:
var cautionArrayString = JSON.stringify(arry);
var dangerArrayString = JSON.stringify(arry1);
I'm using a JavaScript XMLHTTPRequest to POST this data to a php file as follows:
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);
The php file then reads:
$caution = $_POST['caution'];
$danger = $_POST['danger'];
I'm aware at this point I should decode the JSON again upon originally doing so I ended up with a broken array. Here's what the console.log reads AFTER posting the data to the php file, with the php file using:
echo($_POST['caution']);
echo ($_POST['danger']);
Console.log shows:
[{"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[]
Where you can see AT THE END that the 1st array is incomplete and the 2nd is empty (as it should be!). So my question here is, what's causing this and how can I fix it?
Things to Note
I use 3 URL's as inputs to test. The data is complete with 1 URL, but 2 others (the example above included) return this incomplete JSON, seemingly because of a query string being a part of a URL returned from the API?
I console.log(mozResponse) and the output is as expected
I console.log(arry) and console.log(arry1) AFTER iterating through mozResponse and pushing data from mozResponse to arry/arry1 and the output is a complete array
I console.log(arry) and console.log(arry1) AFTER applying JSON.stringify but BEFORE posting to php and the output is complete
Things I've explored
I originally thought this was the result of one of the URL's that the
API returns including a query string (e.g. ?feed=rss2 where it
breaks) however prior to this break point there are query strings
being handled fine
Doesn't seem to be a JSON error as I used
json_last_error(); and it returned 0.
Also doesn't seem to be a a
JSON/POST char limit thing because it returns broken JSON when I
input a different URL (the output from the $_POST for this URL also
breaks following a query string, not sure if this is coincidence)
Suhosin isn't present
Created a phpinfo page to check max_vars but the limit is large
Any help is greatly appreciated!
*different quotes are from copy/pasting!
You probably just need to encode your values for use in a query string:
var cautionArrayString = encodeURIComponent(JSON.stringify(arry));
var dangerArrayString = encodeURIComponent(JSON.stringify(arry1));
var queryString = "email=" + encodeURIComponent(Email) + "&caution=" + cautionArrayString + "&danger=" + dangerArrayString;
Converting it to json does not automatically encode it correctly for use in a url so characters in your values could break the query string.
Assuming the different quotes are caused by copy-pasting...
I have a servlet where I send JSON response (back to javascript) . For example , my response looks like
{
"responseStr":"1,5,119.8406677,7,7,116.5664291,10,10,116.6099319,20,10,117.2185898,25,3,115.2636185"
}
Now what is happening at the moment is that I am collecting data( numbers above) in servlet and sending it in JSON response as a String with comma separated values. When this response reaches front end, all these numbers have to go in a javascript array (where I do my further logic). Currently I am doing this by
var res = JSON.parse(REQ.responseText);
var myArr = res.responseStr.split(',');
My thinking is that the second line( where I use split()) is causing a bottleneck in my application . A few data points as in above example are not a trouble but it becomes a problem when i have thousands of data points.
So my question is that is there a way that when I am creating my response in servlet that I can create the response as javascript array so that I do not have to use split() at all?
Any better ways of achieving the above task of converting the response into javascript array?
If you send responseStr as an Array, when the JSON parses it, it will be an array. So you could send your JSON response as "[1,2,3,4,5,6,7]" and so one, and when you JSON.parse, it will return an array.
To make it a little more clear :
var arr = [1,2,3,4,5];
arr = JSON.stringify(arr); // "[1,2,3,4,5]" -- String
arr = JSON.parse(arr); // [1,2,3,4,5] -- Array
In your response set content-type JSON/application and send JSON array
{
"responseStr":["1","5","119.8406677","7","7","116.5664291","10","10","116.6099319","20","10","117.2185898","25","3","115.2636185"]
}
Then in your JavaScript you can simply use (reference):
var myArray = responseJSONObject.responseStr;
You may utilize JSON.js for various tasks.
That is a great question. JSON can return an array as simply as
{ "responseStr": [[1], [2], [3], [4] }
Cool!
Double Quotes are not necessary unless you want them as strings.
One more thing, you can have multi dimensional arrays too!
{ "responseStr": [[1,10], [2,20], [3,30], [4,40]] }
This link is a great reference:
http://json.org/fatfree.html