user3297094
user3297094

Reputation: 37

Is this invalid JSON?

I wrote this code:

(function($) {
 var url = 'http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za-   android/?json=get_all_posts&callback=?';

 $.ajax({
     type: 'GET',
     url: url,
     async: false,
     jsonpCallback: 'callback',
     contentType: "application/json",
     dataType: 'jsonp',
     success: function(json) {
         console.log(json.status);
     console.log(json);
     try{
       json = $.parseJSON(json);
       alert(json);
     }catch(e){
       alert('invalid');
     }
     },
     error: function(e) {
         console.log(e.message);
     }
 });

 })(jQuery);

http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za-android/?json=get_all_posts&callback=? That is JSON url

I get 'Invalid'... If I remove try and catch i get this error: http://prntscr.com/2sf8bd

Upvotes: 1

Views: 83

Answers (2)

Krish R
Krish R

Reputation: 22711

Try this, Removed $.parseJSON, because of dataType: 'jsonp', already defined

(function($) {
 var url = 'http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za-android/?json=get_all_posts&callback=?';

 $.ajax({
     type: 'GET',
     url: url,
     async: false,
     jsonpCallback: 'callback',
     contentType: "application/json",
     dataType: 'jsonp',
     success: function(json) {
         console.log(json.status);
         console.log(json);

     },
     error: function(e) {
         console.log(e.message);
     }
 });

 })(jQuery);

Upvotes: 1

Skadi2k3
Skadi2k3

Reputation: 66

As Paulloz indicated your json is already a JSON object: JSfiddle without parse and alert: http://jsfiddle.net/Skadi2k3/cKUD7/

Upvotes: 0

Related Questions