views:

46

answers:

1

Why are my AJAX requests failing? I have a website

www.foo.com, and I'm running Tomcat on the same server, www.foo.com:8080/services. However, when a file on foo.com makes an ajax call as such:

$.get("http://foo.com:8080/services/action.do", null, myCallback );

I get an error response, rather than the xml document I get if I manually browser to the url given above. What could be going wrong here?

+1  A: 

As Tomcat is running on a differemt port (8080 as opposed to 80) it's regarded as a different origin, so you're running into the same-origin policy:

Mozilla considers two pages to have the same origin if the protocol, port (if one is specified), and host are the same for both pages.

(My emphasis).

Although this comes from the Mozilla docs, the policy is implemented the same way in all browsers - well, all browsers that are safe to use ;-)

NickFitz
Hard to say if this is really the cause as OP mentioned about an "error response" which is too ambiguous. It might have been a 405 method not allowed, or a 404 page not found, or 500 internal server error. Besides, the browser concern only applies on sharing the session/coookie information.
BalusC
Nope, the same-origin policy applies to Ajax calls (more specifically, requests made using XMLHttpRequest). The jQuery `$.get()` method doesn't return any useful information beyond the word "error" IIRC, so that would be why there isn't any more information.
NickFitz
I'm getting HTTP 200 with an error. I guess I'll just have to serve my files off of tomcat as well, if I hope to make requests against Tomcat. I'll try it.
Stefan Kendall
And that's not a mistake. I get HTTP 200 as a response, but my ajax call hits the error handler.
Stefan Kendall
Yup. If I serve the files over Tomcat, it works. What a pain. I really think this is a poor behavior. I know I can trust my server, so why limit what ports I can connect on?
Stefan Kendall