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
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.
Avatar of Badotz
Badotz
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of larsan
larsan

ASKER

That, and the additional code in the fiddle made my day. Thanks!
Avatar of Jon Norman
Jon Norman
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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

ASKER

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

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

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo