[web2py] Re: json empty with jquery
First of all thanks so much for your help, Leonel. I finally got it. I had a concept mistake. I had to declare a variable to get the data of the client wiht the same structure of the data. I used datos = str(request.vars), it is correct because I can retrieve data: {'key':'value ','key2':'value2'} as a string, but it is not the proper way. The proper way is datos = str(request.vars.key),str(request.vars.key2)] So I can get the values at the client with data.datos[0], data.datos[1] That is solved. The second issue I had was tha I wanted to send the values to the controller, to store them in a database. I finally use the jquery set val () and send it through a form field. $(#test).val(data.datos[0]) That's it. El miércoles, 17 de diciembre de 2014 13:30:03 UTC+1, Leonel Câmara escribió: What do I mean what do I suggest I see nothing wrong with it. Do you want to change the HTML after getting the response? If it's that you can do something like this: {{extend 'layout.html'}} h2Template default/test2.html/h2 dl dtKey/dt dd id=key/dd dtKey2/dt dd id=key2/dd /dl script $(document).ready(function(){ $.ajax({ type: 'POST', url:'http://127.0.0.1:8000/MFREG/default/test2.json', contentType: application/json; charset=utf-8, data: {'key':'value','key2':'value2'}, dataType: 'json', success: function(data){ for (var key in data) { $('#' + key).text = data[key]; } } }); }); /script -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Sorry, but they are not stored in request.vars as expected. It works fine in the alert window, but I can't retrieve the values in the controller to handle them. I have tried with a diferent function in the controler, like this: def test2(): result=testpost() return locals() def testpost(): datos=str(request.vars) key=request.vars.key return locals() The view: {{extend 'layout.html'}} h2Template default/test2.html/h2 {{=BEAUTIFY(response._vars)}} script $(document).ready(function(){ $.ajax({ type: 'POST', url:'{{=URL('default','testpost.json',args='datos')}}', data: {'key':'value','key2':'value2'}, dataType: 'json', success: function(data){ alert(Data:+ data.datos); } }); }); /script And the result: https://lh5.googleusercontent.com/-PIXsDgJWotI/VJFcBhXJLTI/ArI/YBS5zRL7dvA/s1600/Selecci%C3%B3n_808.png Maybe I have to declare key and key2 as var in the script? Because the values I get in the request.vars are Storage{} instead of {'key':'value','key2':'value2'} as desired. So they are not stored in request.vars El martes, 16 de diciembre de 2014 19:57:30 UTC+1, Leonel Câmara escribió: It should, but it should also be in request.vars which is the standard place to deal with it, you should have request.vars.key and request.vars.key2. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Jacinto they are in request.vars otherwise you wouldn't see them in the alert window. Let's walk through what is going on here: 1. You open the address in the browser http://127.0.0.1:8000/MFREG/test2 as the extension is omitted web2py assumes HTML and your HTML view is processed 2. That URL doesn't have any vars so the HTML made by BEAUTIFY response._vars shows them empty 3. When the document is ready your browser makes an ajax request that does have vars. 4. The server answers in json because you used the .json extension and you get the result in the success function in its data argument, you then show it in an alert. As you can see the HTML is never changed after your initial request - that didn't have vars - hence why it keeps showing you exactly that. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Yes, so what do you suggest?. Do I have to fill my request.vars before the document is loaded? How can I do it? El miércoles, 17 de diciembre de 2014 12:43:30 UTC+1, Leonel Câmara escribió: Jacinto they are in request.vars otherwise you wouldn't see them in the alert window. Let's walk through what is going on here: 1. You open the address in the browser http://127.0.0.1:8000/MFREG/test2 as the extension is omitted web2py assumes HTML and your HTML view is processed 2. That URL doesn't have any vars so the HTML made by BEAUTIFY response._vars shows them empty 3. When the document is ready your browser makes an ajax request that does have vars. 4. The server answers in json because you used the .json extension and you get the result in the success function in its data argument, you then show it in an alert. As you can see the HTML is never changed after your initial request - that didn't have vars - hence why it keeps showing you exactly that. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
What do I mean what do I suggest I see nothing wrong with it. Do you want to change the HTML after getting the response? If it's that you can do something like this: {{extend 'layout.html'}} h2Template default/test2.html/h2 dl dtKey/dt dd id=key/dd dtKey2/dt dd id=key2/dd /dl script $(document).ready(function(){ $.ajax({ type: 'POST', url:'http://127.0.0.1:8000/MFREG/default/test2.json', contentType: application/json; charset=utf-8, data: {'key':'value','key2':'value2'}, dataType: 'json', success: function(data){ for (var key in yourobject) { if (yourobject.hasOwnProperty(key)) { console.log(key, yourobject[key]); } } } }); }); /script -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
What do you mean it's empt? It's an object. Try and change your alert to alert(Data: + data.datos); -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
I mean, I try to send data: {'key':'value','key2':'value2'} so I expect to get {'key':'value','key2':'value2'} in the controller. I've tried your suggestion and that's the result: https://lh5.googleusercontent.com/-HUzPxb4Z-ik/VJBcmcKfUqI/Aqo/aOUHGUqv_FY/s1600/Selecci%C3%B3n_805.png I get the same that in the datos var, in the controller. But, the Storage{} list is empty. El martes, 16 de diciembre de 2014 15:54:37 UTC+1, Leonel Câmara escribió: What do you mean it's empty? It's an object. Try and change your alert to alert(Data: + data.datos); -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Well it's supposed to be empty. You're calling the controller like this: http://127.0.0.1:8000/MFREG/default/test2.json This URL has neither args nor vars. Then you do this: datos = str(request.vars) datos2= request.args(0) request.vars is an empty Storage so when you convert it to string you get Storage(), datos2 is None because there is no request.args(0) -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Ok, I.m getting closer. So I change my url call, like this url:'{{=URL('default','test2.json',args='data.datos')}}', but I still have an empty reques.vars. How should I call the controller to pass the data field as a json from the view scritp to the controller? What I am looking for is to send the position.coords.latitude or vars like these from the browser to a controler in server. ... El martes, 16 de diciembre de 2014 17:57:07 UTC+1, Leonel Câmara escribió: Well it's supposed to be empty. You're calling the controller like this: http://127.0.0.1:8000/MFREG/default/test2.json This URL has neither args nor vars. http://127.0.0.1:8000/MFREG/default/test2.json Then you do this: datos = str(request.vars) datos2= request.args(0) request.vars is an empty Storage so when you convert it to string you get Storage(), datos2 is None because there is no request.args(0) -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Humm request.vars shouldn't be empty because you're sending them in data. Add a print request.vars to the controller and see what's there. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
I did with {{=BEAUTIFY(response._vars)}} and the result is: datos:Storage {} So request.vars is empty. If I change the post method for a get method, I can see the correct result at the alert window, but not in the controller, I mean it works in the client side: Wiht get method: https://lh5.googleusercontent.com/-_9jLacZBDWM/VJBw0zK5TRI/Aq4/KN46UfH72LU/s1600/Selecci%C3%B3n_806.png The alert window is ok but the datos var an empty storage objetc. El martes, 16 de diciembre de 2014 18:33:17 UTC+1, Leonel Câmara escribió: Humm request.vars shouldn't be empty because you're sending them in data. Add a print request.vars to the controller and see what's there. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Well {{=BEAUTIFY(response._vars)}} Won't see anything because the vars are only there in the ajax request. That's why it works in your alert. It should work with POST too. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
Yes, it should. I don't know why not. Maybe I'll try with jquery... El martes, 16 de diciembre de 2014 18:59:18 UTC+1, Leonel Câmara escribió: Well {{=BEAUTIFY(response._vars)}} Won't see anything because the vars are only there in the ajax request. That's why it works in your alert. It should work with POST too. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
You're already using jquery. I think the problem is that you're specifying the contenttype. Remove that line and let it use formencode. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
YES!!! Finally. Thanks so much. The only thing now is how to handle the string in the controller. I guess it should be stored in reques.post_vars El martes, 16 de diciembre de 2014 19:36:04 UTC+1, Leonel Câmara escribió: You're already using jquery. I think the problem is that you're specifying the contenttype. Remove that line and let it use formencode. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.
[web2py] Re: json empty with jquery
It should, but it should also be in request.vars which is the standard place to deal with it, you should have request.vars.key and request.vars.key2. -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.