Passing an array of multiple base64 strings - javascript

There are base64 strings in an array that I need to send via jquery's $.post() to a php.
var base64s = [........];
$.post("getbase64.php",
{
'base64s': base64s
},
php:
$base64s = $_POST['base64s'];
//and used as `$base64s[$i]` inside a for loop.
It works only when there are max 2 base64 strings. Is there a maximum size for an array? or what might be the reason?

Related

Compare two objects in cypress which are base64

I'm saving image base64 into variable(base64Image) and would to compare with the image(local) in base64 format but I'm finding error.
var base64Image=res.body.image;
cy.readFile('cypress/e2e/Testdata/Uber Label.png','base64').then((original) => {
base64Image.should('eq',original);});
I'm expecting it to pass or help me how should I compare both values in cypress

How to convert array of images (from input field) into json string

I have an array of images created with
var a = Array.from(document.getElementById("id").files);
then I want to make a JSON string of that array with
var b = JSON.stringify(a);
but it produces nothing but an empty array. As I read this is a common problem with stringify but I couldn't find a solution.
Assuming your array of images are proper File objects, they are a type of binary data called blob.
JSON.Stringify won't work out of the box with blobs, but here is an answer with some workarounds: JSON.stringify or how to serialize binary data as base64 encoded JSON?

JSON data being received in an incomplete format in php?

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...

Cannot retrieve data from javascript json object created in javascript

I have a servlet where it returns a string like [["one","two"],["one","two"]], which is an array representation.
This was sent to the front end using the Gson library's toJson() method as a String. It sends this String array along with some other attributes in JSON format and then I'm using the toJson() method to send it to front end.
In the Ajax response handling, I'm doing a $.parseJSON() on the object and retrieving the values. (It works fine and I was able to retrieve that array string.)
Now in JavaScript I need to create another JSON object with some other values along with this string array and send it to another JavaScript method. But in that method I cannot retrieve the values. My motive is to extract this array string as an array and iterate it.
Following is what I do in JavaScript to send to JavaScript method:
var streamId = stream + CONSTANTS.colon + streamVersion;
var eventsData = {};
var jsonData = [];
eventsData ["source"] = streamId;
eventsData ["data"] = eventsArray;
jsonData.push(eventsData);
onSuccessFunction(jsonData);
In onSuccessFunction I tried to get jsonData.data, but it says undefined.
Any suggestions?

create javascript array in json response in Java

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

Categories

Resources