Answer from server ajax()

100 views
Skip to first unread message

Константин Комков

unread,
Aug 4, 2018, 6:03:59 AM8/4/18
to web2py-users
Hello!

Can you take advice, how can I understan than answer from server was received in JS?
Controller:
def swed():
    ajaxAnswer = 0
    return dict(ajaxAnswer=ajaxAnswer)
def VcodeToDbAndMail():
    ajaxAnswer = 1
    return dict(ajaxAnswer=ajaxAnswer)
JS
<script>
jQuery('#myform').submit(function() {
    var x = 0;
    ajax('{{=URL("VcodeToDbAndMail")}}',['email'], ':eval');
    $('#code').css('visibility','visible');
    x = {{=ajaxAnswer}};
    console.log(x);
  return false;
});
</script>
In both cases x = 0. I think it because console.log has started before ajax was ended.
Also, if i don't return ajaxAnswer in action "swed", I have error: name 'ajaxAnswer' is not defined. 
But in example: Chapter 11 - Eval target, if we using ':eval' I don't see that action "one" in controller return something.

Thank you!

Константин Комков

unread,
Aug 8, 2018, 10:45:43 AM8/8/18
to web2py-users
Are there somebody?

суббота, 4 августа 2018 г., 13:03:59 UTC+3 пользователь Константин Комков написал:

Anthony

unread,
Aug 8, 2018, 11:03:23 AM8/8/18
to web2py-users
On Saturday, August 4, 2018 at 6:03:59 AM UTC-4, Константин Комков wrote:
Hello!

Can you take advice, how can I understan than answer from server was received in JS?
Controller:
def swed():
    ajaxAnswer = 0
    return dict(ajaxAnswer=ajaxAnswer)
def VcodeToDbAndMail():
    ajaxAnswer = 1
    return dict(ajaxAnswer=ajaxAnswer)
JS
<script>
jQuery('#myform').submit(function() {
    var x = 0;
    ajax('{{=URL("VcodeToDbAndMail")}}',['email'], ':eval');

':eval' is only relevant if the controller returns a string of Javascript code, which your controller actions do not appear to be doing.
 
    $('#code').css('visibility','visible');
    x = {{=ajaxAnswer}};

Note, the value of x in the line above will not be updated dynamically based on the value of ajaxAnswer set on the server. Rather, this value is set once on the server when the original page containing this Javascript code is first served.

You'll need to explain more clearly what you are really trying to do.

Anthony

Константин Комков

unread,
Aug 8, 2018, 12:04:17 PM8/8/18
to web...@googlegroups.com
I want make very simple thing. When user load page fist time variable x must be 0. After ajax action variable x must be equal 1. And important moment, that variable x must be returned in JavaScript function.
среда, 8 августа 2018 г., 18:03:23 UTC+3 пользователь Anthony написал:

Val K

unread,
Aug 8, 2018, 2:19:12 PM8/8/18
to web2py-users
It is impossible to return ajax-response from your handler directly
without the use of async/await, supported by only modern browsers. The traditional way is to register a callback that will do all the work

Константин Комков

unread,
Aug 8, 2018, 3:11:47 PM8/8/18
to web...@googlegroups.com
I can do that without web2py functions using xmlhttprequest, but I thougth that web2py have special function. 

8 авг. 2018 г. 21:19 пользователь "Val K" <valq...@gmail.com> написал:

--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/wnyN0bjG2OE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Aug 9, 2018, 10:27:21 AM8/9/18
to web...@googlegroups.com
Controller:

def VcodeToDbAndMail():
   
return 1

Javascript:

<script>
let x
= 0

function showCode(data) {
  x
= parseInt(data);

  $
('#code').css('visibility', 'visible');
}

jQuery(function() {
  jQuery
('#myform').submit(function(e) {
    e
.preventDefault();
    ajax
('{{=URL("VcodeToDbAndMail")}}', ['email'], showCode);
 
});
});
</script>

The third argument of the ajax() function can be a callback function that takes the response from the server. Above, the server simply returns the value "1", which gets passed to the showCode() callback, which sets the value of x and then makes the "#code" element visible.

Anthony

Константин Комков

unread,
Aug 9, 2018, 10:46:03 AM8/9/18
to web2py-users
Thank you, I thought that third parametr of ajax function might be only 'target' or ':eval'.

четверг, 9 августа 2018 г., 17:27:21 UTC+3 пользователь Anthony написал:

Anthony

unread,
Aug 9, 2018, 11:02:38 AM8/9/18
to web2py-users
On Thursday, August 9, 2018 at 10:46:03 AM UTC-4, Константин Комков wrote:
Thank you, I thought that third parametr of ajax function might be only 'target' or ':eval'.

I don't think it's documented in the book. There is also a fourth "options" argument, which is an object -- it's "done" property can be an array of callback functions to be called in order (so, you can use 'target' or ':eval' as the third argument and still have one or more callback functions via the "options" argument).

Anthony
Reply all
Reply to author
Forward
0 new messages