Catch a JSON using Javascript

Asked

Viewed 149 times

2

Django creates a file called py views., in which I have the following function:

def carro_view(request,carro_slug=None):
    evento = Evento.objects.filter(time = '11:28:30').distinct()
    data = []
    for x in evento:
        data.append({
        "latitude": x.lat.replace(',', '.'),
        "longitude": x.lon.replace(',', '.'),
        "title": x.carro         
        })
    marker = {"markers": data }
    return HttpResponse(json.dumps(marker), content_type="application/json")

I wonder how I could get this JSON in a JS file, I know there is a getJSON(url, function(data)), but how could I use this to rescue the return of the function in py views.??

  • 2

    I do not understand the question. If you own in urls.py a routing associating a particular URL to that function, just use that URL in getJSON (probably using a second argument to choose the car, like getJSON(url, { carro_slug:... }, function(data)) and returning the already decoded Javascript object will be available in the parameter data. (P.S. Assuming you refer to $.getJSON jQuery - I don’t know another)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.