permaculture playing cards*
The moose likes HTML, CSS and JavaScript and the fly likes ItemXMLHttp is working with plain HTML page but not with JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "ItemXMLHttp is working with plain HTML page but not with JSP" Watch "ItemXMLHttp is working with plain HTML page but not with JSP" New topic
Author

ItemXMLHttp is working with plain HTML page but not with JSP

Rana Dey
Greenhorn

Joined: Jul 05, 2012
Posts: 10
I have a JSP page hosted on tomcat and calls a web interface (webservice hosted on some other server) from javascript but it gives access denied issue.

However, when we use same JavaScript code which calls the webservice in a plain HTML page, it works. I am still unable to locate the problem, please help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

By the time a JSP page gets to the browser, it's just an HTML page like any other. So you can get the markup of the non-working page (View Source from browser), and compare it to the markup of the working page to see what's different.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

P.S. Access denied usually means you are trying to make a cross-domain request, which is not allowed.
Rana Dey
Greenhorn

Joined: Jul 05, 2012
Posts: 10
The code is the same javascript code but one is with plain HTML (not hosted on tomcat) and other is with JSP page. The later one is not working.

But in general, a webservice will always be hosted on some remote server and possibly placed on a different domain. The client JSP page will however work on local domain.

Regards,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

The code is the same javascript code but one is with plain HTML (not hosted on tomcat) and other is with JSP page.


Again, the fact that one page is a JSP is irrelevant. The browser has no knowledge that the HTML was generated on the server by JSP. If the JavaScript is identical on the pages, then they would both work. So there must be a difference at the browser.

And again, you can;t make an Ajax request to a domain other then that which served the page. You'll need to use a server-side proxy, or JSONP if all you are making are GET requests.
Rana Dey
Greenhorn

Joined: Jul 05, 2012
Posts: 10
you can;t make an Ajax request to a domain other then that which served the page.


In this case even the port needs to be same?? for eg. if the page on browser has been fetched from http://ip:8080/, then the other request for http://ip:8443/ is also subject to cross domain issue??

Regards
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

What happened when you tried it?
Rana Dey
Greenhorn

Joined: Jul 05, 2012
Posts: 10
I received access denied.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Question answered.
Rana Dey
Greenhorn

Joined: Jul 05, 2012
Posts: 10
just to add, when we are trying to access the webservice (from Javascript) through HTTPS, its showing error as Access denied. But when we are accessing the same webservice (from Javascript) through HTTP, its working.

The above behavior is with JSP implementation running on Tomcat (not the standalone html page). SO, I suspect is it really the cross domain issue as you pointed.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript

Eric
 
jQuery in Action, 2nd edition
 
subject: ItemXMLHttp is working with plain HTML page but not with JSP