Trying to get/parse json(p) with jQuery

larsan
larsan used Ask the Experts™
on
Hi.

My first attempt att parsing json is frustrating. Because jQuery "fails silently" on json errors, I'm not getting anywhere.

Anyway. I want to retrieve summary and image_src for each item from this json URL - http://www.gp.se/kulturnoje/film?m=json (which I have no control over, and I cannot change it's formatting)
I'm doing this from the same domain, so that I should not have any cross domain issues.
However, I get nothing.

This is my jQuery so far...
(function($) {
    var url = 'http://www.gp.se/kulturnoje/film/?m=json';
    $.ajax({
		type: 'GET',
		url: url,
		async: false,
		jsonpCallback: 'mktmp.appendSpots',
        contentType: "application/json",
        dataType: 'jsonp',
        success: function(json) {
        	console.dir(json);
        }
	});
})(jQuery);

Open in new window


I've also tried with this.
$.getJSON("http://www.gp.se/kulturnoje/film/1.939463-dark-shadows?m=json&callback=mktmp.appendSpots",
  function(data) {
	alert(data);
  })
  .success(function() { console.log("Oh my - success"); })
  .error(function(jqXHR, textStatus, errorThrown) {
          console.log("error " + textStatus);
           console.log("errorThrown " + errorThrown);
      })
  .complete(function() { console.log("complete"); });</script>

Open in new window

which gives me this in the console:
error parsererror jsonb.html:18
errorThrown SyntaxError: Unexpected token m jsonb.html:19
complete

Open in new window


Please assist.

Best regards,
David
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kiran SonawaneProject Lead
Top Expert 2011

Commented:
It looks the the data you are getting from http://www.gp.se/kulturnoje/film?m=json is not valid json. You can test the json using http://jsonlint.com/

Author

Commented:
As I understand it, I should be able to use it, but as jsonp.
Top Expert 2007

Commented:
There us too much whitespace - a double-quote followed by 2 "\n" chars - hence validation fails.

After you GET the JSON, clean it up before using it.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Information Systems Manager
Top Expert 2012
Commented:
try removing the jsonpCallback parameter, that call is already defined in the source of http://www.gp.se/kulturnoje/film/?m=json:

(function($) {
    var url = 'http://www.gp.se/kulturnoje/film/?m=json';
    $.ajax({
		type: 'GET',
		url: url,
		async: false,
		/*jsonpCallback: 'mktmp.appendSpots',*/
        	contentType: "application/json",
        	dataType: 'jsonp',
        	success: function(json) {
        		console.dir(json);
       		}
	});
})(jQuery);

Open in new window


http://jsfiddle.net/acGCQ/4/

Author

Commented:
That, and the additional code in the fiddle made my day. Thanks!
Jon NormanInformation Systems Manager
Top Expert 2012

Commented:
ahhh cool, so it was the mktmp.appendSpots function you were missing:

    mktmp={
        appendSpots:function(obj){
          alert(obj);
        }
    }

Author

Commented:
Yup. I'm new to all this "callback" business.

Or rather, I had no idea how to create the function...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial