20

First of all, I've been looking for the answer to my problem in several topics and I couldn't find a solution that works with my code.

I'm trying to get the answer from a servlet, if I go to http://XXXZZZ/Servlet/Login?login=pepe&pass=1234 I receive valid JSON as expected:

{"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"}

But when I try the same with $.ajax, I get 2 errors.

$.ajax({
    type : "Get",
    url :"http://XXXZZZ/Servlet/Login",
    data :"login="+login+"&password="+pass,
    dataType :"jsonp",
    success : function(data){
    alert(data);},
    error : function(httpReq,status,exception){
    alert(status+" "+exception);
    }
});

First error (in the popup window):

parsererror Error: jQuery17104145435250829905_1336514329291 was not called

Second error (in the Chrome console):

Uncaught SyntaxError: Unexpected token : Login 1

(And there is the JSON I'm waiting for).

P.S. I have to use dataType : "jsonp", because if I use "json" I also have problems with the Cross-Domain.

dda
  • 6,030
  • 2
  • 25
  • 34
Pablo Postigo
  • 201
  • 1
  • 2
  • 5
  • Whats the error function for ? – adeneo May 08 '12 at 22:22
  • 1
    You cannot *force* JSONP. It has to be supported and return by the server. If it doesn't you are out of luck. If you have control over the server, make it support JSONP. – Felix Kling May 08 '12 at 22:25
  • i'm also programming the server ;) – Pablo Postigo May 08 '12 at 23:09
  • This may not affect this answer but may help other users- often times a parameter of the uri allows you to set the file format. If you are receiving this error, make sure that it is set to `format=jsonp`! – Gaʀʀʏ Dec 13 '12 at 05:39

3 Answers3

21

If you are using jsonp then the syntax is wrong

You need to return

myJsonMethod({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"});

and also add to your ajax request options

jsonp: false,
jsonpCallback: "myJsonMethod"

so

$.ajax({
    type : "Get",
    url :"http://XXXZZZ/Servlet/Login",
    data :"login="+login+"&password="+pass,
    dataType :"jsonp",
    jsonp: false,
    jsonpCallback: "myJsonMethod",
    success : function(data){
        alert(data);},
    error : function(httpReq,status,exception){
        alert(status+" "+exception);
    }
});

(and of-course fix the success as @voyager noted)

Community
  • 1
  • 1
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • So in the server i have to change `static JSONObject Login(String login, String password) throws JSONException{` to `static String Login(String login, String password){ ... json.put("id", id); json.put("login", login); json.put("key", key); return("myJsonMethod("+json+");");` – Pablo Postigo May 08 '12 at 22:48
  • @PabloPostigo, you can use the [`JSONPObject` class](http://jackson.codehaus.org/1.5.3/javadoc/org/codehaus/jackson/map/util/JSONPObject.html) – Gabriele Petrioli May 08 '12 at 22:51
  • had a similar problem. adding jsonp: false was what fixed things for me – Luke Nov 25 '17 at 21:22
11
succes : function(data){

That's a typo:

success : function(data){
Esteban Küber
  • 36,388
  • 15
  • 79
  • 97
1

First off you have a typo in your success parameter; you missed the ending s. Also, when you are performing a JSONP request you need to return your JSON information in JSONP format; which should include the callback token as part of the return string. A sample JSONP string would look likes this:

yourcallbacktoken({"id":3,"login":"pepe","key":"0D1DBA4BE87E02D43E082F9AA1ECFDEB"})

Take a look at this page for more information on the JSONP specifications: http://devlog.info/2010/03/10/cross-domain-ajax/

Shawn Lehner
  • 1,293
  • 7
  • 14