jigsaw
jigsaw

Reputation: 1

jquery ajax with servlet

I had a problem when using servlet with jquery ajax. When I put the html file which contains the js code in the same project with the servlet, it will work. However, when I used this html on another machine and used the URL:http://192.168.1.5:8084/****/Servlet for the ajax, I could not get anything.

$.ajax({
   url:'http://192.168.1.5:8084/****/Servlet',
   data: ajaxdata,
   type:'GET',
   dataType:'text/html',
   //contentType: "text/html",
   success:function(json) { }
});

So any ideas? Thanks.

Upvotes: 0

Views: 3384

Answers (4)

BalusC
BalusC

Reputation: 1108537

If you have control over the servlet, set the HTTP Access-Control headers. This way you can control from the server side on whether the client who has fired the XMLHttpRequest is allowed to process the response. Any recent (and decent) webbrowser will take action accordingly.

Here's an example:

response.setHeader("Access-Control-Allow-Origin", "*"); // Everone may process the response.
response.setHeader("Access-Control-Allow-Methods", "GET"); // Commaseparated string of allowed request methods.

An alternative is JSONP, see also this article.

Upvotes: 3

Bozho
Bozho

Reputation: 597016

This is because you are making cross-domain ajax. Browsers tend to forbid this, because it is a security problem.

See here and here.

(Obviously, you will not have any problems when they are on the same server)

Upvotes: 0

SLaks
SLaks

Reputation: 887215

You cannot use AJAX to send a request to a different site.

Upvotes: 0

kobe
kobe

Reputation: 15835

!jigsaw

this is called same origin policy problem in ajax, It will work if both are on the same server.

read this link , its very nice

Ways to circumvent the same-origin policy

http://www.petefreitag.com/item/703.cfm

search in stack over flow you will get lot of answers

Upvotes: 0

Related Questions