Hi,
> ...like this:
> $('fullName').value = json.fullName;
> for an returned parameter fullName and an html element ID of fullName.
It would be helpful if you gave a more thorough explanation of what
you're looking to do. But, just for the sake of an example, let's
assume:
1. You are sending back properly-formatted JSON with the correct MIME
type in response to an Ajax.Request.
2. The JSON data will have a success flag and firstName and lastName
properties, like this:
{
'success': true,
'firstName': 'Joe',
'lastName': 'Bloggs'
}
3. You want to display the names in the format "firstName lastName" in
an element that already exists on the page with the ID "fullName".
Here's how you can do that:
function showError(errmsg) {
alert(errmsg); // or whatever
}
function showName(elm, obj) {
elm.update(json.firstName + " " + json.lastName);
}
new Ajax.Request(url, {
// Success handler
onSuccess: function(response) {
var json, elm;
// Get the object containing the data from the request.
// Since you returned it with the correct content type
(application/json),
// Prototype has _already_ deserialized it for you and put it
in the
// member 'responseJSON'. (You don't need to use evalJSON()
unless
// you're not setting the content type correctly.)
json = response.responseJSON;
// Check that the JSON was returned
if (!json || !json.success) {
showError("Couldn't get the name.");
} else {
// Get the "fullName" element
elm = $('fullName');
if (!elm) {
// Show an error? The element is missing.
} else {
// Set its content from the JSON data
showName(elm, json);
}
}
},
// Failure handler
onFailure: function(response) {
showError("Error getting the name from the server.");
}
});
Hopefully that'll get you headed the right way. Although it doesn't
talk about JSON, this article[1] on the unofficial wiki may also be
helpful in terms of bulletproofing your Ajax requests.
[1]
http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-requests
HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available