11 JSON

¡Supera tus tareas y exámenes ahora con Quizwiz!

Enter a string that can be given to JSON.parse which produces an array of three values: 1, 7, and 19.

"[1, 7, 19]" A JSON array is created with brackets surrounding the values, which are separated by commas. The JSON array should be surrounded by double quotes to create a string value.

What is the result of the following JSON.stringify call that uses two spaces for the space parameter? JSON.stringify({a:{b:1, c:3}}, null, ' '}

'{ "a": { "b": 1, "c": 3, } }'

When should the loadend handler be used?

Called after the browser receives the response. The loadend handler is called upon both response success and failure, and is called after all other XMLHttpRequest handlers.

When should the result handler be used?

Called after the progress handler to indicate the result status of the response.

When should the timeout handler be used? What is an optional value that can be provided before the request is made? Is it provided by default?

Called if the browser takes too much time to fully receive a response to a request. The timeout is an optional value that can be provided before the request is made. By default, the browser does not provide a timeout for a request

When should the progress handler be used?

Called one or more times while a response is being received by the client. Progress handlers are called before result handlers. The progress handler can be used to provide a data download progress indicator to the user. A similar handler is available to provide an indicator for uploaded data.

When should the loadstart handler be used?

Called when the browser begins to send a request. The loadstart handler is called before any other XMLHttpRequest handler.

When should the error handler be used?

Called when the browser does not receive an appropriate response to a request. Ex: The browser is unable to connect to the server, the connection between browser and server is cut in the middle of a response, etc.

What is JSON?

JavaScript Object Notation is an efficient, structured format for data based on a subset of the JavaScript language.

What is an XMLHttpRequest? What is the benefit of using XMLHttpRequest object?

Object for communicating with web servers using Ajax. Allows web browsers to hide the communication latency and continue to provide a responsive user interface while waiting for a server response.

What is the purpose of JSON.parse method's second parameter? When is it helpful?

Optional parameter for a reviver function. Reviver function is used to modify parsed values before being returned. Helpful when a JSON string represents a data type not available in JSON.

What is a JSON array?

Ordered list of zero or more JSON values separated by commas and enclosed within brackets. Ex: [13, "blue"]

What type of quotes should be used around JavaScript strings containing JSON notation? Why?

Single quotes so that the double quotes for strings and JSON object names do not need to be escaped. Ex: Use '{"name" : "Bob"}' instead of "{\"name\" : \"Bob\"}"

What are the 5 basic types in JSON?

Strings, numbers, objects, arrays, and values

Which optional parameter can convert the string representation of a date into a JavaScript Date object?

The optional reviver parameter takes a key and value pair and returns a JavaScript representation of the value.

What is a JSON object?

Unordered list of zero or more name/value pairs separated by commas and enclosed within braces. E: {"Name" : "Joe", "Age" : 35}

When does an asynchronous request occur?

When the web application sends a request to the server and continues running without waiting for the server response.

What is the result of the following JSON.stringify call? JSON.stringify({a:"one", b:"two", c:"three"}, {["a", "c"]})

'{"a":"one", "c": "three"}' When the replacer parameter is an array, the JSON.stringify method only populates the final string with keys matching values in the array.

What is Ajax?

Asynchronous JavaScript and XML is a technique to asynchronously communicate with a server and update a web page once the response is received without reloading the whole web page.

When should the abort handler be used?

Called when the browser is told to stop a request/response that is still in progress. Ex: The user closes the web page that made the request.

When should the load handler be used? Why?

Called when the exchange between the browser and server has completed. From the browser's perspective, the server received the request and responded. However, the request might not have been successful because of a problem such as a non-existent web page. The HTTP status code must be examined to check which type of response was received. Ex: 200 vs. 404. The load, error, and abort handlers are mutually exclusive and are called after any progress handlers.

What is a JSON value?

Can be one of seven possibilities: a JSON object, a JSON array, a string, a number, a boolean, or null.

What does the following code do? JSON.stringify(new Date ('2016-08-06'))

Converts the JavaScript Date object to the string 2016-08-06T00:00:00.000z by calling the Date object's toJSON method.

What are the steps for using the XMLHttpRequest API?

Create a new XMLHttpRequest object. Assign handlers to the desired events via the addEventListener() method. The addEventListener() method takes two arguments: the event name and the event handler, code that should execute when the event occurs. If the handlers are not set up prior to calling the open method, the progress events will not execute. Initialize a connection to a remote resource using the open() method. The open() method takes two arguments: the HTTP request type and the URL for the resource. Most browsers only support "GET" and "POST" request types. Modify the default HTTP request if needed by using the setRequestHeader() method. Ex: xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") sets the Content-Type header so a URL-encoded string may be sent in a POST request. Send the HTTP request via the send() method. For POST requests, the data to be sent with the request is passed as the argument to the send() method.

What does JSON.parse method do?

Creates a JavaScript object from a string containing JSON.

What does the JSON.stringify do?

Creates a string representation of any passed object by either calling the object's toJSON method if defined or recursively serializing all enumerable, non-function properties.

How many optional parameters does the JSON.stringify method have? What are their purpose?

Has two optional parameters: a replacer and a spacer. Replacer enables customization of generated string. Spacer controls the indentation spacing of output JSON string, which indicates the depth of values.

Which method is typically used with data received from a server: JSON.parse or JSON. stringify?

JSON.parse

Which method is typically used with data sent to a server: JSON.parse or JSON. stringify?

JSON.stringify

What are the three XMLHttpRequest progress handlers?

Loadstart, loadend, and progress handler.

What does the following code return? JSON.stringify({a:1, b:2}, null, ' ')

Returns the string below: '{ "a": 1, "b": 2 }' When the spacer parameter is not specified and not an empty string, the output will also include newlines.

When the spacer parameter is not specified and not an empty string, what happens?

The output will also include newlines.

What does the following code do: JSON.parse( '[ 1, "two", null ]')?

Turns the passed JavaScript string '[ 1, "two", null]' into a JavaScript array containing three values, [ 1, "two", null].

What is Ajax used to transmit?

plain text, HTML, XML, and JSON

What does the following code return? JSON.stringify({a:1, b:2, c:3}, ['a', 'b'])

'{"a":1, "b":2}'

What string is returned from the call to JSON.stringify? JSON.stringify({date: new Date("2001-01-01")})

{ "date": "2001-01-01T00:00:00.000Z" } A JavaScript object such as Date is converted to a string.

Write a JavaScript object that will be converted to '{"a":true,"b":[ ],"c":null}' by JSON.stringify.

{"a": true, "b": [], "c" : null} A JavaScript object is created with braces surrounding the name/value pairs separated by commas.


Conjuntos de estudio relacionados

Ch 11: Alcohol- The Most Popular Drug

View Set

Gender equality - Why does it matters?

View Set

Cell Biology Chapter 19 Sexual Reproduction and Genetics

View Set

Bridging Differences in Cross-cultural Communication

View Set