1

I've a problem with an AJAX GET call using jQuery.

Here's my code:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script>
$(document).ready(function() {    

    $.ajax({
        url : "http://localhost:8080/aplus-framework-webapp/reportServlet?",
        data: "STAT_START_DATE=20131001&STAT_END_DATE=20131031&CAMPAIGN_START_DATE=2013-10-31&CAMPAIGN_END_DATE=2013-10-01&ORDER=Stato",
        dataType: "json",
        type: "GET",
        processdata: true, 
        success : function (data) {
            alert("IN");
        },
        error : function (richiesta,stato,errori) {
            alert("NOT SUCCESS");    
        }
    });// end ajax call
}); // end ready
</script>

The servlet reportServlet is my Java servlet running in localhost that return a JSON:

{"url":"http://d1p0y6pjyasam8.cloudfront.net/PGBANNER/text/20131105100823campaigns.csv"}

I test the page in local but I always see the alert reporting 'NOT SUCCESS'.

I'm new to JS, anyone have any idea on which could be my mistake?

Thanks

Alessio

Roy M J
  • 6,926
  • 7
  • 51
  • 78
AlessioG
  • 576
  • 5
  • 13
  • 32
  • 1
    what is the url of your test page – Arun P Johny Nov 05 '13 at 09:16
  • @user your json gives error on validation: Parse error on line 1: "Nome";""Ragione soc ^ Expecting '{', '[' please fix it and tell us if it works later – Hasan Alaca Nov 05 '13 at 09:27
  • http://localhost:8080/aplus-framework-webapp/reportServlet , check whether this URL works , remove the ? from the URL , and try to Lert error in the failure block – Hussain Akhtar Wahid 'Ghouri' Nov 05 '13 at 09:27
  • Instead of just printing `NOT SUCCESS`, print the actual error. The API gives this to you in the `error` callback! You have called it `errori`, so print that. – andyb Nov 05 '13 at 09:29
  • I modify my Java servlet adding in the response header: Access-Control-Allow-Origin:* Now I can see the alert 'IN' and the json that my servlet return. – AlessioG Nov 05 '13 at 09:30
  • @user917651 did you validate json? – Hasan Alaca Nov 05 '13 at 09:31
  • possible duplicate of [How do I send an AJAX request on a different port with jQuery?](http://stackoverflow.com/questions/2099728/how-do-i-send-an-ajax-request-on-a-different-port-with-jquery) – andyb Nov 05 '13 at 14:01

4 Answers4

1

Are Sure Your servlet return header json ?

user2857877
  • 125
  • 1
  • 10
  • I modify my Java servlet adding in the response header: Access-Control-Allow-Origin:* Now I can see the alert 'IN' and the json that my servlet return. – AlessioG Nov 05 '13 at 09:29
  • I specify the conten type of the response in my header but i didn't specify the param: Access-Control-Allow-Origin. So my header was wrong we can say. Thanks! – AlessioG Nov 05 '13 at 09:32
  • So did removing the question mark actually solve the problem? – andyb Nov 05 '13 at 10:36
  • No, it's nothing to do with headers. The AJAX request was violating the [`Same-origin policy`](http://en.wikipedia.org/wiki/Same_origin_policy) which was fixed by configuring the server to `Access-Control-Allow-Origin:*` – andyb Nov 05 '13 at 14:00
1

If the website you're requesting from and the servlet you're requesting on do not have the same port (for instance 80 and 8080), it will break the Same Origin Policy.

See this stackoverflow question form more information and answers.

Community
  • 1
  • 1
Masklinn
  • 64
  • 2
  • I modify my Java servlet adding in the response header: Access-Control-Allow-Origin:* Now I can see the alert 'IN' and the json that my servlet return – AlessioG Nov 05 '13 at 09:44
  • `+1` - this is the correct answer, since the OP reconfigured the server to `Access-Control-Allow-Origin:*` – andyb Nov 06 '13 at 08:41
0

Try removing the question mark at the end of your url

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
  • Nothing to do. I remove the question mark but the result is the same Maurice. – AlessioG Nov 05 '13 at 09:22
  • I modify my Java servlet adding in the response header: Access-Control-Allow-Origin:* Now I can see the alert 'IN' and the json that my servlet return. – AlessioG Nov 05 '13 at 09:31
-1

I assume you mean with 'page in local' that you try this in a local file on your hard-drive. This is disabled due to security reasons in mainly all browsers. You can find further information how to disable this in Google Chrome for development purposes here:

http://opensourcehacker.com/2010/11/29/disabling-cross-domain-security-check-for-ajax-development-in-google-chrome/

Markus
  • 192
  • 6
  • I modify my Java servlet adding in the response header: Access-Control-Allow-Origin:* Now I can see the alert 'IN' and the json that my servlet return. – AlessioG Nov 05 '13 at 09:29