[Proto-Scripty] Re: JSON for Novice
onSuccess:function(response). response in this case *receives* the XHR object after which you can query the various methods/functions in it that are returned from the Ajax.Request eg response.responseText, response.responseJSON etc By the way response is just a variable name Hope this helped I stand to be corrected though if im in any way wrong... -Jason On Tue, Sep 30, 2008 at 2:27 AM, Bobby.D [EMAIL PROTECTED] wrote: Ok, I've fixed several of the problems, but something I don't understand: onSuccess: function(response) -- Where does response come from??? --~--~-~--~~~---~--~~ 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 prototype-scriptaculous@googlegroups.com 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 -~--~~~~--~~--~--~---
[Proto-Scripty] Re: JSON for Novice
Ok, I've fixed several of the problems, but something I don't understand: onSuccess: function(response) -- Where does response come from??? On Sep 21, 4:55 am, T.J. Crowder [EMAIL PROTECTED] wrote: Hi, There are several problems there. var url = ajax/addRole.php?newDefName= +name+ amp;type= +type+; What if the name contains characters you can't use in a URL? You want to do some encoding there. Separately, you're doing too much encoding (and the wrong kind) -- why are you using amp; there? First off, URLs aren't encoded with XML/HTML character entities, and second off, you don't *want* to encode a literal ampersand, you want to have an unencoded ampersand in the query string that introduces the 'type' parameter. The syntax of the JavaScript is also invalid in a few ways; any half- decent environment should be calling your attention to them (even IE should be giving you a little yellow exclamation point). You've got an extra plus sign after type at the end of the code quoted above, and you don't have a closing paren on the Ajax.Request constructor call at all. HTH, -- T.J. Crowder tj / crowder software / com On Sep 20, 2:12 am, Bobby.D [EMAIL PROTECTED] wrote: Ok, here is my scriptit's not working. When I comment out the whole ajax request it works, but I just can't figure out what's going on. Maybe I've just looked over it to many times. function addNewRole(type) { var name = prompt(New +type+ Name); var url = ajax/addRole.php?newDefName= +name+ amp;type= +type+; new Ajax.Response(url, { onSuccess: function(response) { var json; json = response.responseJSON; if(!json) { alert(The New Defendant Role Could Not Be Added. Please Contact Your Administrator); } else { if(json.success) { alert(Success!); } } } }; } Thanks in advance! On Sep 18, 6:06 am, bluezehn [EMAIL PROTECTED] wrote: There ya go - knew prototype did that somewhere! Cheers for filling in the gaps T.J. On Sep 18, 11:02 am, T.J. Crowder [EMAIL PROTECTED] wrote: Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! The Ajax.Response object[1] passed to the handlers has a responseJSON property, so if your server-side page returns this JSON: { success: true, message: Defendant added } ...your onSuccess handler can access that like so: onSuccess: function(response) { var json; json = response.responseJSON; if (!json) { // Your server-side code did not return // JSON data as expected; handle that. } else { // Good, got JSON data if (json.success) { // Your server-side code says all went well; // you'd probably show json.message, e.g.: alert(json.message); } else { // Your server-side code ran correctly, but // returned an error; handle that. } } } [1]http://www.prototypejs.org/api/ajax/response HTH, -- T.J. Crowder tj / crowder software / com On Sep 18, 10:33 am, bluezehn [EMAIL PROTECTED] wrote: Whenever a web page is requested and returned to the browser - whether this is through AJAX or a new page view - the server tells the browser what type of content it is being served and whether or not the request was carried out successfully. Prototype can access this information, and indeed does to work out whether to use onSuccess and onFailure. But it can also see what type of content is returned, and if it sees JSON, prototype will automatically decode it into a javascript object for you. You still need php to tell it what the content type is and you do that before any output is made with the following line: header('Content-type: text/json'); You can then return an associative array encoded into json: return json_encode($assocArray); Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! But I remember reading that prototype will do this automatically. The long way round is: onSuccess: function(transport){ var json = transport.responseText.evalJSON(); } And in fact, the prototype short cut is rendered somewhat irrelevant
[Proto-Scripty] Re: JSON for Novice
Hi, There are several problems there. var url = ajax/addRole.php?newDefName= +name+ amp;type= +type+; What if the name contains characters you can't use in a URL? You want to do some encoding there. Separately, you're doing too much encoding (and the wrong kind) -- why are you using amp; there? First off, URLs aren't encoded with XML/HTML character entities, and second off, you don't *want* to encode a literal ampersand, you want to have an unencoded ampersand in the query string that introduces the 'type' parameter. The syntax of the JavaScript is also invalid in a few ways; any half- decent environment should be calling your attention to them (even IE should be giving you a little yellow exclamation point). You've got an extra plus sign after type at the end of the code quoted above, and you don't have a closing paren on the Ajax.Request constructor call at all. HTH, -- T.J. Crowder tj / crowder software / com On Sep 20, 2:12 am, Bobby.D [EMAIL PROTECTED] wrote: Ok, here is my scriptit's not working. When I comment out the whole ajax request it works, but I just can't figure out what's going on. Maybe I've just looked over it to many times. function addNewRole(type) { var name = prompt(New +type+ Name); var url = ajax/addRole.php?newDefName= +name+ amp;type= +type+; new Ajax.Response(url, { onSuccess: function(response) { var json; json = response.responseJSON; if(!json) { alert(The New Defendant Role Could Not Be Added. Please Contact Your Administrator); } else { if(json.success) { alert(Success!); } } } }; } Thanks in advance! On Sep 18, 6:06 am, bluezehn [EMAIL PROTECTED] wrote: There ya go - knew prototype did that somewhere! Cheers for filling in the gaps T.J. On Sep 18, 11:02 am, T.J. Crowder [EMAIL PROTECTED] wrote: Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! The Ajax.Response object[1] passed to the handlers has a responseJSON property, so if your server-side page returns this JSON: { success: true, message: Defendant added } ...your onSuccess handler can access that like so: onSuccess: function(response) { var json; json = response.responseJSON; if (!json) { // Your server-side code did not return // JSON data as expected; handle that. } else { // Good, got JSON data if (json.success) { // Your server-side code says all went well; // you'd probably show json.message, e.g.: alert(json.message); } else { // Your server-side code ran correctly, but // returned an error; handle that. } } } [1]http://www.prototypejs.org/api/ajax/response HTH, -- T.J. Crowder tj / crowder software / com On Sep 18, 10:33 am, bluezehn [EMAIL PROTECTED] wrote: Whenever a web page is requested and returned to the browser - whether this is through AJAX or a new page view - the server tells the browser what type of content it is being served and whether or not the request was carried out successfully. Prototype can access this information, and indeed does to work out whether to use onSuccess and onFailure. But it can also see what type of content is returned, and if it sees JSON, prototype will automatically decode it into a javascript object for you. You still need php to tell it what the content type is and you do that before any output is made with the following line: header('Content-type: text/json'); You can then return an associative array encoded into json: return json_encode($assocArray); Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! But I remember reading that prototype will do this automatically. The long way round is: onSuccess: function(transport){ var json = transport.responseText.evalJSON(); } And in fact, the prototype short cut is rendered somewhat irrelevant here as if the data you're transporting needs to be secure according to a logged in user, you need to make the returned JSON non- executionable, or a default constructor included through XSS could hijack your data. So the correct way is something like this: in PHP: return '/*' .
[Proto-Scripty] Re: JSON for Novice
Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! The Ajax.Response object[1] passed to the handlers has a responseJSON property, so if your server-side page returns this JSON: { success: true, message: Defendant added } ...your onSuccess handler can access that like so: onSuccess: function(response) { var json; json = response.responseJSON; if (!json) { // Your server-side code did not return // JSON data as expected; handle that. } else { // Good, got JSON data if (json.success) { // Your server-side code says all went well; // you'd probably show json.message, e.g.: alert(json.message); } else { // Your server-side code ran correctly, but // returned an error; handle that. } } } [1] http://www.prototypejs.org/api/ajax/response HTH, -- T.J. Crowder tj / crowder software / com On Sep 18, 10:33 am, bluezehn [EMAIL PROTECTED] wrote: Whenever a web page is requested and returned to the browser - whether this is through AJAX or a new page view - the server tells the browser what type of content it is being served and whether or not the request was carried out successfully. Prototype can access this information, and indeed does to work out whether to use onSuccess and onFailure. But it can also see what type of content is returned, and if it sees JSON, prototype will automatically decode it into a javascript object for you. You still need php to tell it what the content type is and you do that before any output is made with the following line: header('Content-type: text/json'); You can then return an associative array encoded into json: return json_encode($assocArray); Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! But I remember reading that prototype will do this automatically. The long way round is: onSuccess: function(transport){ var json = transport.responseText.evalJSON(); } And in fact, the prototype short cut is rendered somewhat irrelevant here as if the data you're transporting needs to be secure according to a logged in user, you need to make the returned JSON non- executionable, or a default constructor included through XSS could hijack your data. So the correct way is something like this: in PHP: return '/*' . json_encode($assocArray) . '*/'; then in JS: onSuccess: function(transport){ var jsObj = transport.responseText.substr(2, transport.responseText - 2).evalJSON(); // chop the comments off start and end } Then json has your javascript object in. For your second issue. From ajax you get a name and id, you insert those into the dropdown. Then, you need a way of selecting the option you just inserted into the select, there are lots and lots of ways to do this. I'm sure there is a better way of doing this, but what I'd do is when I add it, give it an id. You can use Element#Identify() or just use it's id prefixed by some arbitrary string. Then something like this: $$('select#selectID option').each(function(option) { option.selected = false; // make sure nothing else is set to selected }); $('idofOptionYouJustAdded').selected = true; I haven't tested any of the code in this, but the basis of all of it should be right. Come to think of it I've never seen selected used like that as a property of a JS object, apart from the googling I literally just did! On Sep 17, 11:22 pm, Bobby.D [EMAIL PROTECTED] wrote: I've fiddled with Prototype/Script.aculo.us and want to start using JSON when interacting with my PHP Code. So far I'm good, I've got some data being passed to my PHP, it gets process and then encoded to JSON. Now, I need to figure out how to use that data in my onSuccess functionCan anyone help? Also, this is a side now, but I've got a drop down box populated from a DB and an option to add to the list. I'm using just the AJAX request to make the change. Basically, user click Add New Defendant a JS Prompt appears, the user enters the new title, upon submit the AJAX gets called inserts the new defendant name into the database and return the Name (which I've already got) and the id (this is important because it's the value of each select...) I am looking to change the selected index of the drop down box to match what the user just entered, but I can't figure out how to do it. I know this isn't a Prototype or Script.aculo.us issue, but if anyone has a suggestion it would be much appreciated. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Prototype script.aculo.us group. To
[Proto-Scripty] Re: JSON for Novice
There ya go - knew prototype did that somewhere! Cheers for filling in the gaps T.J. On Sep 18, 11:02 am, T.J. Crowder [EMAIL PROTECTED] wrote: Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! The Ajax.Response object[1] passed to the handlers has a responseJSON property, so if your server-side page returns this JSON: { success: true, message: Defendant added } ...your onSuccess handler can access that like so: onSuccess: function(response) { var json; json = response.responseJSON; if (!json) { // Your server-side code did not return // JSON data as expected; handle that. } else { // Good, got JSON data if (json.success) { // Your server-side code says all went well; // you'd probably show json.message, e.g.: alert(json.message); } else { // Your server-side code ran correctly, but // returned an error; handle that. } } } [1]http://www.prototypejs.org/api/ajax/response HTH, -- T.J. Crowder tj / crowder software / com On Sep 18, 10:33 am, bluezehn [EMAIL PROTECTED] wrote: Whenever a web page is requested and returned to the browser - whether this is through AJAX or a new page view - the server tells the browser what type of content it is being served and whether or not the request was carried out successfully. Prototype can access this information, and indeed does to work out whether to use onSuccess and onFailure. But it can also see what type of content is returned, and if it sees JSON, prototype will automatically decode it into a javascript object for you. You still need php to tell it what the content type is and you do that before any output is made with the following line: header('Content-type: text/json'); You can then return an associative array encoded into json: return json_encode($assocArray); Now in prototype, it does automatically decode this for you. I can't remember off the top of my head where it goes! But I remember reading that prototype will do this automatically. The long way round is: onSuccess: function(transport){ var json = transport.responseText.evalJSON(); } And in fact, the prototype short cut is rendered somewhat irrelevant here as if the data you're transporting needs to be secure according to a logged in user, you need to make the returned JSON non- executionable, or a default constructor included through XSS could hijack your data. So the correct way is something like this: in PHP: return '/*' . json_encode($assocArray) . '*/'; then in JS: onSuccess: function(transport){ var jsObj = transport.responseText.substr(2, transport.responseText - 2).evalJSON(); // chop the comments off start and end } Then json has your javascript object in. For your second issue. From ajax you get a name and id, you insert those into the dropdown. Then, you need a way of selecting the option you just inserted into the select, there are lots and lots of ways to do this. I'm sure there is a better way of doing this, but what I'd do is when I add it, give it an id. You can use Element#Identify() or just use it's id prefixed by some arbitrary string. Then something like this: $$('select#selectID option').each(function(option) { option.selected = false; // make sure nothing else is set to selected }); $('idofOptionYouJustAdded').selected = true; I haven't tested any of the code in this, but the basis of all of it should be right. Come to think of it I've never seen selected used like that as a property of a JS object, apart from the googling I literally just did! On Sep 17, 11:22 pm, Bobby.D [EMAIL PROTECTED] wrote: I've fiddled with Prototype/Script.aculo.us and want to start using JSON when interacting with my PHP Code. So far I'm good, I've got some data being passed to my PHP, it gets process and then encoded to JSON. Now, I need to figure out how to use that data in my onSuccess functionCan anyone help? Also, this is a side now, but I've got a drop down box populated from a DB and an option to add to the list. I'm using just the AJAX request to make the change. Basically, user click Add New Defendant a JS Prompt appears, the user enters the new title, upon submit the AJAX gets called inserts the new defendant name into the database and return the Name (which I've already got) and the id (this is important because it's the value of each select...) I am looking to change the selected index of the drop down box to match what the user just entered, but I can't figure out how to do it. I know this isn't a Prototype or