I'm not familiar with website design, so I wrote the util.js because I 
thought it might make things a bit simpler to handle. If I wanted a delete 
function, I would just add that in util.js and call the doPost() with the 
correct callback function. The doPost() would pass that to the respective 
callback function in web2py's default controller as a string. I use 
simplejson library to parse it to a python dict. 

Could you expand further by what you mean with sending "actual data"? I did 
the doPost() because all I wanted was a button which will pass information 
back to the server for processing. Most of the data isn't user-warranted, 
that is, the user doesn't have to enter anything. All the item properties 
(like the id) are already on that page so when the user clicks on "Add Me", 
all that's needed on the server is which item id it is dealing with as well 
as some other property. Since it's just two parameters which doesn't 
involve user input at all (the user shouldn't have to type in the item id 
just to add it), I thought using json would be easier than creating an 
entire form and submitting that way.

If the preferred 'web2py way' is to create a form for all data transactions 
between the controller and the view, could you provide a quick example of 
how to do it in this scenario with just one button (an anchor tag)?

On Monday, May 6, 2013 5:46:14 PM UTC-4, Derek wrote:
>
> You don't normally send just json data directly to a server. jquery will 
> post data to a server to make it appear that a form was submitted. 
> Generally as ""application/x-www-form-urlencoded"
> So it's a key:value format which is better than json anyway.
> If you did want to send a json string, you'd assign that string to a 
> variable and have your post method use that variable (as you are doing). 
> However, there is no need, unless you want to make your application go 
> through those hoops for no reason...
>
> Why is it you are sending json instead of the actual data in the first 
> place? When you test it yourself, are you giving a json string?
> Is jquery actually posting the string or is it unpacking the 'data' as 
> json before submitting it?
>
> Instead of reading the request body, why not reference the variable 
> 'request.vars.data' , or request.post_vars.data ?
>
> On Monday, May 6, 2013 12:23:51 PM UTC-7, brac...@gmail.com wrote:
>>
>> Thanks Anthony, I had to move all the web2py tags out of the static 
>> util.js and I put them in <script> inside the view html. The quotes was the 
>> problem.
>>
>> I can call my controller now and I see the json that was passed to it, 
>> which is great.
>>
>> The current views code now looks like this:
>>
>> <script>
>>     $(document).ready(function() {
>>       $('#additem').click(function() {
>>            var url = '{{=URL('default', 'add_item')}};
>>            add_item(url, {{=item.id}}, 1);
>>       }
>> </script>
>>
>>
>>
>> <a href="" id="additem">Add Me</a>
>>
>> I have two questions now:
>>
>> 1. For some reason every click results in a javascript alert saying that 
>> it Failed, even though I can successfully do stuff with the data in the 
>> controller. Is there some sort of code I'm supposed to return through the 
>> controller to let jquery know that it's been successful?
>>
>> 2. To be safe, I'll sanitize the expected integers like so:
>>
>>     def add_item():
>>          # get json data as 'data' ...
>>          item_id = int(data['test']['item_id'])
>>          other_data = int(data['test']['some_detail'])
>>    
>>          # Insert that item_id and other_data into db...
>>    
>>     But if I expected a string, how would I sanitize that string before 
>> using it to do something with the database? Is there a safe practice for 
>> this type of approach of getting data from json?
>>         
>> On Monday, May 6, 2013 2:26:02 PM UTC-4, Anthony wrote:
>>>
>>>
>>> <a href="" id="additem" onclick="add_item({{=URL('default', 'add_item'
>>>> )}}, {{item.id}}, 1);">Add Me</a>
>>>>
>>>
>>> Again, you're going to need quotes around that URL:
>>>
>>> <a href="" id="additem" onclick="add_item('{{=URL('default', 'add_item
>>> ')}}', {{item.id}}, 1);">Add Me</a>
>>>
>>> Anthony
>>>
>>>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to