How to make this code work ...

94 views
Skip to first unread message

Ben Duncan

unread,
Jun 4, 2019, 3:23:11 PM6/4/19
to Web2py
Ok, the following is code from w3 schools:

The code is as follows :

<!DOCTYPE html>
<html>
<body>

<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();
}
</script>

</body>
</html>

The question is how do I get this code to run under web2py ?

Thanks ...

Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

Anthony

unread,
Jun 4, 2019, 5:35:09 PM6/4/19
to web2py-users
What are you trying to do? That code should work fine in web2py, as it's just Javascript. You just need to have it call a web2py URL. Of course, web2py includes some built-in mechanisms for handling Ajax requests, such as the ajax() Javascript function and the LOAD() helper to create Ajax components.

Anthony

Ben Duncan

unread,
Jun 4, 2019, 6:16:12 PM6/4/19
to Web2py
I've been trying to run it ... When you click on the button it does nothing

Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/9b9fa219-3038-4d61-9206-a5ab134f783e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dave S

unread,
Jun 4, 2019, 9:00:44 PM6/4/19
to web2py-users


On Tuesday, June 4, 2019 at 3:16:12 PM UTC-7, Ben Duncan wrote:
I've been trying to run it ... When you click on the button it does nothing

Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division




When I replace the "ajax-info.txt" with a real URL (that is, one for a web2py function on my server, and which returns a txt file), I see the network access, including a response with HTTP status 303.  For some reason (cookie management?) it doesn't work for me to login via a separate tab, and each time I click the button I get a different cookie.  I've been poking at sending credentials, but I still get the 303 response and I apparently am not setting the Authorization header as I think I am, but only setting Access-Control-Request-Headers to "authorization".  And there is also "allow origin" issues, because file://tmp/test.html comes from a different address than the server.

HTH
/dps


To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

Dave S

unread,
Jun 4, 2019, 9:18:18 PM6/4/19
to web2py-users
$


On Tuesday, June 4, 2019 at 6:00:44 PM UTC-7, Dave S wrote:


On Tuesday, June 4, 2019 at 3:16:12 PM UTC-7, Ben Duncan wrote:
I've been trying to run it ... When you click on the button it does nothing

Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division




When I replace the "ajax-info.txt" with a real URL (that is, one for a web2py function on my server, and which returns a txt file), I see the network access, including a response with HTTP status 303.  For some reason (cookie management?) it doesn't work for me to login via a separate tab, and each time I click the button I get a different cookie.  I've been poking at sending credentials, but I still get the 303 response and I apparently am not setting the Authorization header as I think I am, but only setting Access-Control-Request-Headers to "authorization".  And there is also "allow origin" issues, because file://tmp/test.html comes from a different address than the server.

HTH
/dps



The cross-site and authorization issues go away when you host the file on the server (say, .../applications/myapp/static/test.html)

/dps
 

Val K

unread,
Jun 5, 2019, 1:11:43 AM6/5/19
to web2py-users
Try xhttp.withCredentials = true

Dave S

unread,
Jun 5, 2019, 5:05:36 AM6/5/19
to web2py-users


On Tuesday, June 4, 2019 at 10:11:43 PM UTC-7, Val K wrote:
Try xhttp.withCredentials = true

I did.  The tricky part is that using a file on the browser's host, the first request is OPTIONS.

/dps
 

Val K

unread,
Jun 5, 2019, 6:30:46 AM6/5/19
to web2py-users
Yes, file:// is even another protocol, so using http transport in this case is out of all standards. But it should work fine with regular web2py app (over http)

Ben Duncan

unread,
Jun 5, 2019, 8:54:23 AM6/5/19
to Web2py
Put some console logs in to see  ....strange ...

This site makes use of a SHA-1 Certificate; it’s recommended you use certificates with signature algorithms that use hash functions stronger than SHA-1.
In function loaddoc loadajax:14:11
readystate= 1 loadajax:18:11
status= 0 loadajax:19:11
This site makes use of a SHA-1 Certificate; it’s recommended you use certificates with signature algorithms that use hash functions stronger than SHA-1.
readystate= 2 loadajax:18:11
status= 404 loadajax:19:11
readystate= 3 loadajax:18:11
status= 404 loadajax:19:11
readystate= 4 loadajax:18:11
status= 404

ajax_info is just a text file that Iv'e created that looks like :
<p>Ajax is not a programming language</p>
<p>
  <h1>AJAX</h1>
  AJAX IS A technique for accessing webs ervers from a web page
</p>
<p>AJAX stands for ASYNCHRONOUS Javascript and XML/JSON</p>

Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division
On Wed, Jun 5, 2019 at 5:30 AM Val K <valq...@gmail.com> wrote:
Yes, file:// is even another protocol, so using http transport in this case is out of all standards. But it should work fine with regular web2py app (over http)

--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/70288247-9e3b-4078-81e5-385ef4a4f67e%40googlegroups.com.

Dave S

unread,
Jun 5, 2019, 3:45:00 PM6/5/19
to web2py-users

And where did you put the file?  From the 404, it wasn't where your server-side was looking for it.

My file was in the myapp/uploads directory, and I had "download/" in the URL (the fun things at the bottom of controllers/default.py work for me pretty much without change).

(I moved the test html+javascript into static, and explicitly loaded it it with "myapp/static/test.html".  Putting it on the server got rid of the xsite issues.)

/dps



 

On Wed, Jun 5, 2019 at 5:30 AM Val K <valq...@gmail.com> wrote:
Yes, file:// is even another protocol, so using http transport in this case is out of all standards. But it should work fine with regular web2py app (over http)

--
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 web...@googlegroups.com.

Dave S

unread,
Jun 5, 2019, 3:47:51 PM6/5/19
to web2py-users


On Wednesday, June 5, 2019 at 3:30:46 AM UTC-7, Val K wrote:
Yes, file:// is even another protocol, so using http transport in this case is out of all standards. But it should work fine with regular web2py app (over http)

Since I found a way to successfully demonstrate the javascript working, it is quite possible I will not ever pursue the file:// x-site issues.  I've done OPTIONS handling in node.js before, and I think I can see how to do it in the front end (nginx), but I don't have a reason to pursue it at present.

/dps
 

Dave S

unread,
Jun 5, 2019, 10:51:02 PM6/5/19
to web2py-users

 Although looking through some of my long-open tabs, I see mcm's cors_origin decorator from October '16, and curiosity might provoke me.

-d

Ben Duncan

unread,
Jun 6, 2019, 8:03:54 AM6/6/19
to Web2py
Ok, moving it to static makes it work fine. It was in view/default  
Any IDEAS on what / why this works this way ?

Thanks ...

Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division

--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/ac268300-8ec7-4f4d-9d3b-9ffbe7f12f4f%40googlegroups.com.

Dave S

unread,
Jun 6, 2019, 3:52:16 PM6/6/19
to web2py-users


On Thursday, June 6, 2019 at 5:03:54 AM UTC-7, Ben Duncan wrote:
Ok, moving it to static makes it work fine. It was in view/default  
Any IDEAS on what / why this works this way ?

Thanks ...

Ben Duncan
DBA / Chief Software Architect
Mississippi State Supreme Court
Electronic Filing Division


View isn't really a typical search path ... it's tied to the controller/function naming system.

/dps
 

On Wed, Jun 5, 2019 at 9:51 PM Dave S <snide...@gmail.com> wrote:


On Wednesday, June 5, 2019 at 12:47:51 PM UTC-7, Dave S wrote:


On Wednesday, June 5, 2019 at 3:30:46 AM UTC-7, Val K wrote:
Yes, file:// is even another protocol, so using http transport in this case is out of all standards. But it should work fine with regular web2py app (over http)

Since I found a way to successfully demonstrate the javascript working, it is quite possible I will not ever pursue the file:// x-site issues.  I've done OPTIONS handling in node.js before, and I think I can see how to do it in the front end (nginx), but I don't have a reason to pursue it at present.

/dps
 

 Although looking through some of my long-open tabs, I see mcm's cors_origin decorator from October '16, and curiosity might provoke me.

-d

--
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 web...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages