views:

38

answers:

1

Hi there,

I was trying to create a prototype object with four attributes: 'name', 'basis' and 'rows', which are values taken from a form and 'head' which should be an array of string values.

Classdef:

var TableTemplate = Class.create();  
 TableTemplate.prototype = {  
    initialize: function(name, basis, head, rows) {  
        this.name = name;  
        this.basis = basis;  
        this.head = head;    
        this.rows = rows;  
   },  
};

It should be passed to a .php file in the backend as following:

function sendRequest() {  
 var sorting = doSorting();  
 //alert(sorting.inspect());  
 var table = new TableTemplate($F('templateName'), $F('basisTemplate'), sorting , $F('maxRows'));  
 new Ajax.Request("test.php",   
  {   
  method: 'post',   
  postBody: 'table='+ Object.toJSON(table),  
  onComplete: showResponse   
  });  
}

where 'doSorting()' returns a stringarray.

Problem: I can't seem to get other attributes than the head (array) in the object, or this one kinda overwrites the others... When I try to get a name or basis, the array will be returned.

Has anyone an idea how to solve this? Thanks in advance for your help,

Mara

A: 

(Caveat: I'm not a PHP person, and it has some built-in JSON handling that I'm not familiar with. But I'm fairly familiar with Prototype.)

You're specifying the postBody of the request, which means you're in charge of making sure it's correctly encoded. What you're sending will look something like this:

table={"name": "some template", "basis": "some head", "head": ["blah", "blah", "blah", "blah"], "rows": 15}

Depending on the contents, it may not be encoded as you're expecting at the other end.

Rather than specifyihng the postBody, I'd probably just send it as a parameter and let the framework worry about escaping things for me:

new Ajax.Request("test.php",   
  {   
  method: 'post',   
  parameters: {table: Object.toJSON(table)},  
  onComplete: showResponse   
  });

Then retrieve the JSON string from the "table" parameter on the PHP side and decode it.

If you're still not getting the result you expect, take a look at the JSON string and make sure you're getting what you're expecting to see.

T.J. Crowder
Thanks for your quick answer!I think the problem is solved now.
Mara