0

I am trying to parse a json file using jquery but im not getting any data back,

My jquery code:

$(document).ready(function () {
    $.ajax({
        type: 'GET',
        url: 'http://anteatercycles.co.uk/xml/sensa.json',
        success: function (json) {
            var data1 = json.data[0];
            var result1 = '<h3>' + data1.merchant_category + '</h3>' + '<p>' + data1.description + '</p>' ;
            $('#description1').append(result1);
        }
    });
});

<div id="description1"></div>

Thanks in advanced.

edit to below still not retrieving any data?

$(document).ready(function () {

$.ajax({ type: 'GET', dataType: "json", url: 'http://anteatercycles.co.uk/xml/sensa.json', success: function (json) { json=JSON.parse(json);//parse json data var data1 = json.data[0]; var result1 = '

' + data1.merchant_category + '

' + '

' + data1.description + '

' ; $('#description1').append(result1); } }); });
</script>   

<div id="description1"></div>
Joe
  • 15,669
  • 4
  • 48
  • 83
  • Has your json varibale any value??? – Teo Sep 04 '14 at 11:29
  • Is your JS-file also on anteatercycles.co.uk? If not, see here: https://en.wikipedia.org/wiki/Same-origin_policy – Reeno Sep 04 '14 at 11:29
  • possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – Reeno Sep 04 '14 at 11:30
  • hi yes it is at http://anteatercycles.co.uk/xml/sensa.json –  Sep 04 '14 at 11:30
  • Ajax response is text. You must parse to JSON object. `json = JSON.parse(json); var data1 = json.data[0]; .... ` – Kristiyan Sep 04 '14 at 11:38
  • very new to this has anyone got a fiddle to help me understand? –  Sep 04 '14 at 11:44

4 Answers4

3

Adding in dataType: 'json' as an option on the AJAX call should work. If you tell jQuery it's JSON data, it should parse it into an object for you. It's most likely that the server isn't returning the JSON file with the correct MIME type so jQuery is just assuming it's plain text.

Joe
  • 15,669
  • 4
  • 48
  • 83
3

Use JSON.parse() or set dataType:"json"

 success: function (json)
      {   json=JSON.parse(json);//parse json data
          var data1 = json.data[0];
          var result1 = '<h3>' + data1.merchant_category + '</h3>' +
          '<p>' + data1.description + '</p>' ;
          $('#description1').append(result1);
      }
0

if you are expecting a JSON response from your service you need to give dataType:"JSON" then this will automatically returns a JavaScript object.The JSON data is parsed in a strict manner.any malformed JSON is rejected and a parse error is thrown or may be you are getting empty response from the service.

Shaik Md N Rasool
  • 484
  • 1
  • 5
  • 13
0

Look at the data your server is sending for that JSON:

%  curl -I http://anteatercycles.co.uk/xml/sensa.json
HTTP/1.1 200 OK
Server: Apache
Last-Modified: Thu, 04 Sep 2014 10:41:58 GMT
ETag: "fe1328ea-4ff3e-5023b007f6708"
Content-Type: text/plain
Content-Length: 327486
Accept-Ranges: bytes
Date: Thu, 04 Sep 2014 12:10:03 GMT
X-Varnish: 2518742114 2518618326
Age: 72
Via: 1.1 varnish
Connection: keep-alive

The important line is the Content-Type. It is claiming it is text/plain but it should be saying application/javascript.

You should fix the server so it gives the correct content-type for that file.

As a hacky work-around, you can tell jQuery to ignore the content type and treat it as JSON anyway.

Add dataType: "json" to your ajax options:

$.ajax({
    type: 'GET',
    dataType: "json",

Additionally, the JSON doesn't return an object with a data property. So accessing json.data makes no sense. You want json[0].

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335