Format data using jQuery to send to CakePHP controller

I'm a little new to Ajax / Jquery, so I apologize if this is a simple question, but I just couldn't figure it out.

I am using CakePHP and jQuery.

I want to save the link, label and description into a table by pulling out "innerHTML" from the HTML page. I can not present the data in the expected format - this is the format that the controller expects.

I am extracting data from html, html looks like this:

<div class="listing"> <ul> <li class="link">www.yahoo.com</li> <li class="label">Yahoo</li> <li class="description">This is Yahoo home page</li> </ul> </div> ... 

I can parse the HTML and get my โ€œlinksโ€, โ€œlabelโ€ and โ€œdescriptionโ€.

But when I send data to the controller, I canโ€™t figure out how to get the data in the expected format.

After transferring data to variables using JavaScript (jQuery), I submit it using the following jQuery function:

 $.post("/links/save", {link: link, label: label, notes: description}); 

When data is placed in the controller, the data format is:

 ( [form] => Array ( [link] => www.yahoo.com [label] => Yahoo [description] => This is Yahoo home page ) ) 

The format that the controller expects from data:

 ( [data] => Array ( [Link] => Array ( [link] => www.yahoo.com [label] => Yahoo [description] => This is Yahoo home page ) ) ) 

I know that I can take the data as it is and put it in the correct format in the controller, but it seems that this is not necessary.

Can someone please tell me how to manipulate data in jQuery so that it is placed as the controller expects it?

+4
source share
2 answers

CakePHP still has PHP, so you can just use $_POST .

It might be more convenient to stick to Cake's data structures, so to automatically load $this->data , you should format the data as follows:

 { data : { ModelName : { link : ..., ... } } } 
+1
source
0
source

All Articles