Link to home
Start Free TrialLog in
Avatar of larsan
larsan

asked on

Trying to get/parse json(p) with jQuery

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
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

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/
Avatar of larsan
larsan

ASKER

As I understand it, I should be able to use it, but as jsonp.
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.
ASKER CERTIFIED SOLUTION
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of larsan

ASKER

That, and the additional code in the fiddle made my day. Thanks!
ahhh cool, so it was the mktmp.appendSpots function you were missing:

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

ASKER

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

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