The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. January 07, 2009, 05:26:17 PM

Login with username, password and session length


Pages: 1 [2]
  Print  
Author Topic: My first AJAX project, biting off way more than i can probably chew  (Read 930 times)
nutballs
Administrator
Lifer
*****
Online Online

Posts: 3525


View Profile
« Reply #15 on: July 29, 2008, 01:14:56 PM »

i know.

question..

So you basically use JSON for encoding an array on the server side, so they can then be decoded on the client side, and they are still an array correct?
Or short version, JSON is to encode an OBJECT for transit?

never used JSON so, just making sure. Read a few pages about it and thats what it seems like. Though you can obviously use it to encode functions as well it seems.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5324


:sniffle: Humor was so much easier before.


View Profile
« Reply #16 on: July 29, 2008, 01:43:35 PM »

json is actually a protocol rather than a function set for JS or PHP.

In php it's json_encode and json_decode. It serializes either an object or an array into a string, almost exactly like serialize, except that it is json notation.

In javascript, by saying var arr = eval('(' + aJSONStr + ')'); you are unserializing, that's it. The JSON notation can be used literally as well quite effectively - I've often put big ol' strings onto a variable in JS and then simply eval'd them to liven an array.

The problem is that there is no json_encode in Javascript. So I use a function called stringifyer that is a modified cersion of one I found a while ago. I'll post it in the JS repository in a moment. Here's the handiness: if you have a whole lot of complex data to get back up to the server, make it an array in JS, encode it and send as one big blob rather than trying to emulate this with lots of postParams. Excellent for multiply nested arrays as well. This has completely replaced XML for me because I can send hierarchical data back and forth without any need for parsing.
Logged

If I can't be Mr. Root then I don't want to play.
nutballs
Administrator
Lifer
*****
Online Online

Posts: 3525


View Profile
« Reply #17 on: July 30, 2008, 06:57:45 PM »

ok i get it.

So next question.

cookies, how do you deal with them?

server side doesnt seem to work, which i didnt think it would, so you have to do them client side right? In particular, for cookies that a planted AFTER the page is finished loading, and would be created from an AJAX call.

is that right?
Logged
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Online Online

Posts: 679



View Profile
« Reply #18 on: July 30, 2008, 07:18:27 PM »

bah, don't do that. it'll make your page that much easier to spam with a robot!
Logged

nutballs
Administrator
Lifer
*****
Online Online

Posts: 3525


View Profile
« Reply #19 on: July 30, 2008, 08:21:25 PM »

i have little concern of spamming in this app. It is a zero yield target, so far. There is 1 vector i have thought of, but I have that covered i think (think myspace worm type of situation).

at first i was surprised that you cant set a cookie from the server via the ajax call, but then realized that it is not a complete communcation, the same way an http page load is. So, i guess i have to do client side cookie setting.

I have NEVER done client side cookie handling, so this should be a new experience...
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5324


:sniffle: Humor was so much easier before.


View Profile
« Reply #20 on: July 30, 2008, 10:25:33 PM »

Cookies are a breeze. One of the earlier posts I made here was my personal local storage handler, here it is in the JS repository:

http://www.perkiset.org/forum/javascript_ajax/efficient_local_client_storage_of_cookies-t86.0.html

Quiet easy in fact. Create an instance of the class, set a "storage file name" and set/get name/value pairs. Easy peezie, japaneesie (Channelling my gramps for a moment there). You can only store about 20 cookies normally per domain, but with this class I compress them into a single string so you can use the full 4096 of a single cookie "slot" for any number of cookies. The "storage file name" is the one-of-20 that you are allowed to use per domain.
« Last Edit: July 30, 2008, 10:27:07 PM by perkiset » Logged

If I can't be Mr. Root then I don't want to play.
nutballs
Administrator
Lifer
*****
Online Online

Posts: 3525


View Profile
« Reply #21 on: July 30, 2008, 10:31:51 PM »

actually i figured it out anyway. didnt know about the 20 cookie limit, but no biggy, i am only doing 2 and everything else will be serverside. but i will check out your class because maybe then I will just jam stuff into the cookie instead.

ok i have to share.
This little methodology is working pretty well for me so far. I am sure I will paint myself into a corner at some point, but so far so good
Obviously I am leaving out a bunch of stuff, but the response is JSON.
Code:
function handleAjax(sender)
{
var arr = eval('(' + sender.lastResponse + ')');
for (var i in arr['display'])
{
document.getElementById(arr['display'][i]['parentid']).innerHTML = arr['display'][i]['html'];
}
for (var i in arr['cookie'])
{
Set_Cookie( arr['cookie'][i]['name'], arr['cookie'][i]['value'], arr['cookie'][i]['days']);
}
}
its pretty cool. I can send back an arbitrary number of things, and they all loop and get processed.

idunno i just had to share.

I now have a login,registration,forgot password popup box done. all ajax. yeay me.
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Online Online

Posts: 5324


:sniffle: Humor was so much easier before.


View Profile
« Reply #22 on: July 30, 2008, 10:45:27 PM »

NBs, FTW!

Looks like it's just about all under the hood now mang. Go man go!
Logged

If I can't be Mr. Root then I don't want to play.
Pages: 1 [2]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!