I have a Extjs form which has around 50 fields to search. I am posting form data in JSON format to server. Before sending the data I am appying JSON.stringify method to convert the JavaScript object notation data into JSON form. but it seems after applying the function, the search operation has become slow. Is JSON.Stringify method costlier ? I want to know more details about the performance of the method, Please suggest alternatives for better performance.
Related
I'm trying to use AJAX to receive updates from a database without needing to reload the page. I've had very minimal problems thus far, however, I finally hit one. When retrieving information from a database, I have it convert all possible rows to an array. From there, I encode that array back to Javascript using $test1 = json_encode($array). In this same PHP function, I'm doing this to multiple pieces, converting those encoded arrays to a string by doing echo $test1."#".$test2."#".$test3. In Javascript, I split that string by the hashtag, and then grab the information of an array by doing something like test1 = array[1]. When doing that, I can retrieve that array entry as a string even though it's in the fashion of a Javascript array. Any ideas on how I can make it treat that string, which is setup like an array, as an array? Thanks in advance!
When retrieving the information from the AJAX response, you need to make it a Javascript Object. Convert the response to an object by doing JSON.parse(response) to get it to work
I've read a lot about sanitizing and escaping of untrusted data, e.g. starting with this cheat sheet. But I didnt get the full picture yet. I am struggeling to understand, what principles I have to follow in order to parse JSON data safely.
Let's be more precise by means of an example. I retreive an Object in JSON format via an Ajax call from my server. The object shoudnt contain any malicious code, but.... you never now. So I parse the JSON data using JSON.parse in JS. The sesult is a multilevel JS object. I use the object in various way, e.g. creating table code via JS and writing it in the DOM.
Is this approach safe. Or do I have to somehow escape the invidual items of the object individually? Appreciate your help. Happy to concrete my example if necessary.
Last night I was sending some data to my server via AJAX (specifically, $.post), when I ran into the issue that JavaScript Arrays needed to be "stringified" and applied as a field to an object before being sent. That is:
$.post("/myUrl/", {"myKey": json.stringify(myArray)}, ... );
If I didn't perform the stringify, the format of the POST would get screwed up by the time it reached Django.
Why do arrays need to be stringified for requests when objects can just be sent as is? Or, does this vary wildly on the back-end being used?
The object doesn't get sent "as is". jQuery recodes the object to URL key/values pairs, for example:
http://this.url?myKey=stringifiedArray.
The documentation has more information on this.
data
Type: PlainObject or String
A plain object or string that is sent to the server with the request.
Objects are used because they're an easy data structure for developers to manipulate.
The data I am sending my page is encoded in JSON, parsed using Javascript then displayed in an HTML SELECT element using a loop. The data arrives already sorted, but I am having issues keeping the correct order when decoding the JSON string, which nullifies the sorting applied on the data.
Sample data: {"test":{"4":"first","5":"second","3":"third"}}
Using jQuery's JSON parser and Javascript's eval() function, I am getting the following results:
{"test":{"3":"third","4":"first","5":"second"}}
It is not possible to modify the format of the data and the keys ("4", "5", "3") must remain in the same order. The real data is much more complex, but this sample illustrates very well my issue.
How can I maintain the order of the JSON data when parsing it from Javascript?
Use an array if you want to keep the order. That should be the only way to maintain the order in javascript.
My goal here is to cut down the number of cookies I'm using to store things like persistent states into a single cookie by setting and retrieving multiple key/value pairs within a single cookie using an array.
I'm mostly interested in knowing if someone else has already done this and has written a plugin for it, as I would think this has been done before.
You could serialize the object into a JSON string. That would make it super simple to re-load the object. See question 191881 for information on how to serialize an object to JSON in JavaScript.