5

I have access to a API which is a JSP file and is in JSON format. I am trying to fetch those data from the JSP page to a PHP script and process them and then store in my MySQL sever.

The JSON string is valid in the JSP page I checked in few JSON Formatter and validator online.

This is my code which I am using to get JSON data from the page but, every time my ajax call fails.

$('#button').click(function(e){
var url = 'http://xxxxx:8080/StudentAPI/index.jsp';
$.ajax({
    url : url,
    dataType : 'json',
    success : function(response) {
        alert('Success');
    },
    error : function(request, textStatus, errorThrown) {
        alert(request+textStatus+errorThrown);
    }
});
e.preventDefault();
})

Please help me and any suggestion to do it in a better way is always welcomed.

NewUser
  • 3,729
  • 10
  • 57
  • 79

1 Answers1

3

You are making a cross domain ajax call. So it wont work if you try it just like a normal ajax call.

One way is like to

  1. to set 'Access-Control-Allow-Origin' to '*' at the server end to which you are making the ajax request.

  2. then make a jquery ajax call with the 'crossDomain' attribute 'true' in the settings variable.

Another way would be to use jsonp

depending on on the server you are using you can find how to add cors in this article.

UPDATE

her is a w3c article which describes how to configure cors in java servlets. See the In Java servlets section.

The point is basically that the server which is giving the ajax response should be having "Access-Control-Allow-Origin" field set to "*" in the response headers.

Community
  • 1
  • 1
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
  • How can I set Access control allow origin? Where should I set that? can you please tell in details.. – NewUser May 02 '13 at 05:48
  • The JSP file in a Tomcat server, I checked for .conf files as mentioned in the first article but I didn't find any conf files.. :(. Do I need to allow in the server where I will place my PHP files? – NewUser May 02 '13 at 06:29
  • no need. it should be in the target server or page of ajax call. which in your case i believe is jsp pages on tomcat. PHP has no part in this issue. you can even make a plain html page hosted on a server and initiate your ajax calls from it. – Mithun Satheesh May 02 '13 at 07:08
  • did you check the w3c article? i believ you can do something like `response.addHeader("Access-Control-Allow-Origin", "*"); ` .. sorry if i am wrong as dont have enough exposure to java. – Mithun Satheesh May 02 '13 at 07:11
  • Struggled for the internet connection for a while :(.. Yes I have written response.addHeader.. Still my problem persists.. – NewUser May 02 '13 at 08:04
  • @Dibya : if i were you i would search for how to add a response header in jsp and set the values for `access-control-allow-origin` in the sample code. also refer [this link](http://java-success.blogspot.in/2012/11/cors-and-jquery-with-spring-mvc-restful.html) – Mithun Satheesh May 02 '13 at 08:10
  • @Dibya : no problems. :) – Mithun Satheesh May 02 '13 at 08:29