response.js after redirect

76 views
Skip to first unread message

Filipe Reis

unread,
Jun 29, 2017, 6:49:20 AM6/29/17
to web2py-users
Hello, I was wondering if someone could help me in figuring this out.

I have a view that when the user click on a button opens a modal with a insertion form.

I'm calling it by doing this way:
web2py_ajax_page("GET", url, "", target);

url - being the url like /house/create
target - being the div where the form will appear

on the form validation I'm trying to use this plugin: https://github.com/willsteinmetz/notific8 to show flash messages as it is in javascript when the form is an error I am able to do this:
response.js = "$.notific8('Please fill all necessary fields.', {heading: 'Error'});" 

All works ok in here..

but if I try the same when the form is success does not work because I do a redirect after which resets the page and there is no flash message no more... 

I know that session.flash migh work for this cases, but I'm not able to understand how to make them work together in this case since this notification plugin works using javascript.

Any ideas?

Anthony

unread,
Jun 29, 2017, 7:53:33 AM6/29/17
to web...@googlegroups.com
Are you using this notification plugin for all flash messages on your site? If so, why not just incorporate it into the layout and use response.flash (and session.flash) as usual? In the layout, you could have something like:

<script>
$
.notific8('{{=response.flash}}');
</script>

If you need to pass in options as well, you could make response.flash a dictionary holding both the message and the options and convert the options part to JSON in the layout:

In controller:

    response.flash = dict(message='There is an error.', options=dict(heading='Error'))

In layout:

<script>
var w2p_flash = {{=XML(response.json(response.flash)) if response.flash else 'null'}};
if (w2p_flash) {
    $
.notific8(w2p_flash.message, w2p_flash.options);
}
</script>

Anthony

Filipe Reis

unread,
Jun 29, 2017, 9:25:00 AM6/29/17
to web...@googlegroups.com
Thanks for your time, this seems to be a solution to exactly what I want however when I try to implement it I get this error:
Resource interpreted as Document but transferred with MIME type application/json

Should this be a problem because I am including the web2py javascript also? It needs to be included because of these other stuff like the web2py_ajax_page function and others.


Also when the post is not successful the flash seems to not work this way since the request is trapped? (because was called via the web2py_ajax_page("GET", url, "", target);)

Anthony

unread,
Jun 29, 2017, 11:07:41 AM6/29/17
to web2py-users
You can instead put that code in the view of the controller. If you need it across multiple actions, just create a separate view template for it and use {{include ...}} to include it wherever needed.

Anthony

Filipe Reis

unread,
Jun 29, 2017, 11:25:20 AM6/29/17
to web2py-users
Was looking better it seems that this is a problem with how response.flash passes the value.

var w2p_flash = {&quot;message&quot;: &quot;New client inserted into database.&quot;, &quot;options&quot;: {&quot;theme&quot;: &quot;lime&quot;, &quot;heading&quot;: &quot;Success&quot;}};

This is how the flash is passed to the view, is there any way to get it right with " instead of &quot; since response.json cannot handle it this way?

For the view that is loaded via ajax and not showing the flash would you say that I need to do the same for this particular view?

Thanks for all your time.

Anthony

unread,
Jun 29, 2017, 11:30:30 AM6/29/17
to web...@googlegroups.com
Try wrapping it in XML(). Also, you'll have to import json rather than using response.json (which will change the content-type header).

{{import json}}
var
w2p_flash = {{=XML(json.dumps(response.flash)) if response.flash else 'null'}};

Anthony

Filipe Reis

unread,
Jun 29, 2017, 11:42:38 AM6/29/17
to web2py-users
Yeah that did it, however still getting the same issue: Resource interpreted as Document but transferred with MIME type application/json.

This is for sure happening when using the session.flash with a dict.

Filipe Reis

unread,
Jun 29, 2017, 12:36:42 PM6/29/17
to web2py-users
Got it all working now. It was a problem with using response.json to parse the flash message.

To future reference this is how it is:

var w2p_flash = {{=XML(response.flash) if response.flash else 'null'}};
if (w2p_flash) {
   var w2p_flash = jQuery.parseJSON(JSON.stringify(w2p_flash));
   $.notific8(w2p_flash.message, w2p_flash.options);
}

Anthony

unread,
Jun 29, 2017, 5:57:17 PM6/29/17
to web2py-users
You don't need to do it that way. Just import json -- see https://groups.google.com/d/msg/web2py/9WnoALNHxTI/54cTUgHNBgAJ.

Anthony

Filipe Reis

unread,
Jun 29, 2017, 6:05:02 PM6/29/17
to web2py-users
Ohh nice thinking, I forgot that I could do such thing :) 

Anyways I asked this in other thread but do you know something about this?

How to use signature with web2py_component? (I changed web2py_ajax_page to web2py_component since I saw that this calls the ajax_page)

So when a request is done to the page it only shows if was triggered the that call only. This is what I think that @auth.requires_signature() does

Anthony

unread,
Jun 30, 2017, 10:54:47 AM6/30/17
to web2py-users

You can generate a URL with a user signature (requires login) as follows:

$.web2py.component('{{=URL(..., user_signature=True)}}', ...);

And then decorate the action with @auth.requires_signature().

Anthony
 
Reply all
Reply to author
Forward
0 new messages