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
JavaScriptjQuery

Avatar of undefined
Last Comment
larsan

8/22/2022 - Mon
Kiran Sonawane

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

ASKER
As I understand it, I should be able to use it, but as jsonp.
Badotz

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Jon Norman

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
larsan

ASKER
That, and the additional code in the fiddle made my day. Thanks!
Jon Norman

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

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

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

Or rather, I had no idea how to create the function...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.