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
On Jul 9, 3:37 am, chrysanthe m <[email protected]> wrote:
> Hi David
> Thanks, but I think I one step further. I am assuming an Ajax.request() has
> happened, returned and what I am looking at to understand further is syntax
> like this:
> $('fullName').value = json.fullName;
> for an returned parameter fullName and an html element ID of fullName. I am
> wondering, can I do
> $('fullName').value =processMe( json.fullName);
> which I know the obvious is try it. I will, just trying to get an heads
> up. I will report back. Also I am assuming that prototpye is processing
> that nomenclature $('fullName') segment and just doing a
> document.getElementById. Any insight appreciated.
>
>
>
> On Wed, Jul 8, 2009 at 12:25 PM, David Behler <[email protected]> wrote:
>
> > I guess you would have to use Ajax.Request to process the returned
> > value before updating the designated element.
>
> > But I'm beginner and could be wrong here.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---