redrom
redrom

Reputation: 11642

Problem with GET values from JSON format using Jquery

I trying get some values from remote server, which generate data in JSON format>

$("#productSearchpage").live("pageshow", function(event, ui){

            $.getJSON('http://www.akcniceny.cz/php/mapa-slev-data.php?box[left][lat]=50.5&box[left][lng]=14.5&box[right][lat]=51&box[right][lng]=15&group[]=3&typ[]=zbozi&full=televizor', function(data) {
              $('.result').html('<p>' + data.jmeno + '</p>'
                + '<p>' + data.ulice[1] + '</p>');
            });

But i have not experience with JSON and so tryied used by tutorial on Jquery site, unfortunately script does not work.

What can be worng and how to right USE JQUERY and JSON together?

Thanks for any reply.

Upvotes: 0

Views: 358

Answers (3)

ntziolis
ntziolis

Reputation: 10221

The json returned by the server is not valid.
Basically it returns an array of objects, but after the last object there is an unnecessary comma (which usually indicates that another object is the array).

When constructing the json server side remove the last comma before returning the response.

If you don't have access to the server side code you will not be able to use $.getJSON. You will need flow these steps:

  • Use the $.ajax method with config: dataType: text
  • Use java script string manipulation to remove the trailing comma
  • Parse the string as json using $.parseJSON

Upvotes: 2

Vijay Sirigiri
Vijay Sirigiri

Reputation: 4711

Is this script in the same domain? If not browsers don't allow cross-domain get / post using xmlhttp request. You might have to use other workarounds. Here are the details.

Upvotes: 0

Stoic
Stoic

Reputation: 10754

Please, try this:

$("#productSearchpage").live("pageshow", function(event, ui){
    $.ajax({
        url: 'http://www.akcniceny.cz/php/mapa-slev-data.php?box[left][lat]=50.5&box[left][lng]=14.5&box[right][lat]=51&box[right][lng]=15&group[]=3&typ[]=zbozi&full=televizor',
        dataType: 'json',
        data: data,
        success: $('.result').html('<p>' + data.jmeno + '</p>'+ '<p>' + data.ulice[1] + '</p>');
    });
});

Upvotes: 0

Related Questions