Freddy
Freddy

Reputation: 867

Get data from external JSON file

I'm obtaining data from an API and its JSON is stored on a separate file.

The URL for this file looks like this:

http://events.com/rsvp.php?id=1234

The JSON on the above page looks like this:

{
  rsvp: true
}

The id query string differs for each contact, so id 1234 might have rsvp: true but id 4321 might have rsvp: false.

What I'm trying to do is check the URL, get the value of the rsvp node and then, perform some action.

At the moment, with my request, I'm seeing the log error, unsure why?

<!-- this gets the id field -->
{% set contact_id = request.query_dict.id %}

{% set base_url = "http://events.com/rsvp.php?id=" %}
{% set url = base_url + contact_id %}

<script>

$(function() {

  $.ajax({
    url: "{{ url }}",
    method: "GET",
    dataType: 'json',
    success: function(result){
      console.log(result);
    },
    error:function() {
      console.log("Error")
    }
  });

});

</script>

Upvotes: 0

Views: 44

Answers (1)

Quentin
Quentin

Reputation: 944429

Your JSON is invalid.

Property names in JSON must be strings, not identifiers.

{
  "rsvp": true
}

Generate your JSON with json_encode, not with whatever method you are currently using.

Upvotes: 2

Related Questions