1

I am using following ajax to receive data from server.

$.get("http://***/umbraco/Api/SomeApi/SignIn",{apiVersion : 1,email:"name@gmail.com", password:"mypassword" },function(data) {
   alert('in');
   alert(data);
}, "jsonp");

I could see 200 Ok in inspect element. And i could also see the response text. But i couldn't get data in alert. Both above alerts aren't working.

EDIT

I tried the following ajax:

 jQuery.ajax({  
    type: "GET",  
    url: 'http://***/umbraco/Api/SomeApi/SignIn?apiVersion=1&email=name@gmail.com&password=mypassword&callback=?',  
    dataType: "json",  
    success: function (results) {  
        alert("Success!");  
    },  
    error: function (XMLHttpRequest, textStatus, errorThrown) {  
        alert("error");  
        alert('XMLHttpRequest '+ XMLHttpRequest);
        alert('textStatus '+ textStatus);
        alert('errorThrown '+ errorThrown);
    }  
});  

Results in:

 Status Code: 200 Ok

But,

Parse Error is throwing. How can i fix this?

enter image description here enter image description here

Please Help.

Erma Isabel
  • 2,167
  • 8
  • 34
  • 64

1 Answers1

0

For the service to be called using jsonp, the service has to return jsonp which is actually a javascript code.

If it is a third party service, pls make sure that the service supports jsonp.

Saranya
  • 1,988
  • 16
  • 20
  • I could see all the values in the network tab of inspect element. But why parse error? Response header says that content-type: application/json. Still i couldn't make it work. Is there any issue with my code? – Erma Isabel Oct 30 '13 at 06:46
  • @Erma Isabel, from the comments of your question, i can see that you got "Origin is not allowed by Access-Control-Allow-Origin" error once, which means the service is in a different domain. in that case you have to use jsonp only , supplying a callback with the ajax call. To achieve this your server has to support jsonp, it has to return something like callback({json data});. Also you need to define your call back function in javascript. so pls ensure 2 things - whether the server is in different domain, and if it supports jsonp. then use jsonp. – Saranya Oct 30 '13 at 07:15
  • How do i know it supports jsonp? – Erma Isabel Oct 30 '13 at 07:37
  • http://stackoverflow.com/questions/3467366/how-do-i-know-if-a-server-has-jsonp-turned-on refer this post – Saranya Oct 30 '13 at 07:51
  • When i try that it returns "Access-Control-Allow-Origin error".Does that mean anything? – Erma Isabel Oct 30 '13 at 08:45
  • did u try accessing it directly from browser? – Saranya Oct 30 '13 at 09:43
  • can u post the request url u tried and the response you got? did u append callback function to the url and what is the response u got? – Saranya Oct 30 '13 at 09:47
  • from your response it is obvious that u receive only "true " in response and there is no callback wrapper. so ur service doesnot seem to support jsonp. If you have control over the service you can set the header Access-Control-Allow-Origin to "*" to over come this. – Saranya Oct 30 '13 at 10:04
  • How does that possible? I am using the same origin allowed by the server. – Erma Isabel Oct 30 '13 at 10:17
  • Are you setting the domain to the same origin allowed by the server somewhere? cos from the response i could see that only one origin is allowed.. – Saranya Oct 30 '13 at 10:20
  • 1
    yes, i set up my domain as the allowed origin using mongoose. – Erma Isabel Oct 30 '13 at 10:25
  • but it still returns "Access-Control-Allow-Origin error" when the ajax call happens right? then hope it is something to do with setting domain.. – Saranya Oct 30 '13 at 10:28
  • No, i doesn't, see status code 200 ok. My problem is that, instead of success function, error is triggering. Parse error. If it is "Access-Control-Allow-Origin error", i will not be able to see the response in the inspect element as well. – Erma Isabel Oct 30 '13 at 10:33
  • ok. Are you sure the data returned is json format? i think problem is with format.JQuery might be throwing error if response couldnot be parsed as JSON. check http://stackoverflow.com/questions/6186770/ajax-request-return-200-ok-but-error-event-is-fired-instead-of-success – Saranya Oct 30 '13 at 10:36
  • May be, i am using "dataType: "json" in ajax and in response header it is said that, content-Type : application/json. Does that make sense? – Erma Isabel Oct 30 '13 at 10:38
  • yes. it makes sense. as a final try, can u remove the datatype:json in ajax call as a final try? second, add contenttype:"application/json" and dataType:json and check – Saranya Oct 30 '13 at 10:46
  • When i remove "dataType: "jsonp",", it triggers, "Access-Control-Allow-Origin error". When i add "contenttype:"application/json" and dataType:json", then previous issue, parse error. – Erma Isabel Oct 30 '13 at 10:58
  • So you are saying, there is nothing else to try? Can this be server issue? – Erma Isabel Oct 30 '13 at 10:59
  • I m pretty sure that it is something to do with dataType/contentType.. btw, why are you still using jsonp if your domain is allowed by the server – Saranya Oct 30 '13 at 11:02
  • Oopz..sorry my mistake tried, dataType:json. it still triggers, "Access-Control-Allow-Origin error" – Erma Isabel Oct 30 '13 at 11:05
  • refer http://stackoverflow.com/questions/249692/jquery-wont-parse-my-json-from-ajax-query for a similar issue – Saranya Oct 30 '13 at 11:06
  • Then it is cross origin issue, check if your origin is in the list of allowed origins. – Saranya Oct 30 '13 at 11:09