Question

I'm using python2.7, django1.6, and apache2. I have enabled crossdomain access. I have tried with and without a crsf token. I have no idea what I'm doing wrong.

visiting: url/to/site/contacts/api/v1/adresponses/1 works properly so tastypie is set up. I can see ad responses.

Someone please help. I have been stuck trying to get tastypie to work for days now.

Here is my api.py

class UserResource(ModelResource):
    class Meta:
            queryset = User.objects.all()
            resource_name = 'user'
            fields=['username', 'id']
            allowed_methods = ['get']
            authorization = Authorization()
            authentication = Authentication()

 class AdResponsesResource(ModelResource):
    users = fields.ManyToManyField('contacts.api.UserResource', 'users',
                    related_name='adresponse')
    class Meta:
            queryset = AdResponses.objects.all()
            resource_name = 'adresponses'
            authorization = Authorization()
            authentication = Authentication()

This is my ajax call

$(function (){
    $.ajax({
        url: 'url/to/site/contacts/api/v1/adresponses/1',
        type: 'GET',
        accepts: 'application/json',
        dataType: 'json'
    });

So I'm not entirely sure why my original posted js doesn't work but I saw this example http://django-tastypie.readthedocs.org/en/latest/cookbook.html and it works as expect. Maybe its because I didn't have a success function?

$.ajax({
    url: '../../api/v1/user/',
    contentType: 'application/json',
    type: 'GET', 
    success: function(data, textStatus, jqXHR) {
         // Your processing of the data here.
         console.log(data);
         }
    });
Was it helpful?

Solution

I would need to see your url set up for the api to give you more help, but for starters the original url you provided:

url: 'url/to/site/contacts/api/v1/adresponses/1',

and the one in your answer

url: '../../api/v1/user/',

do not call the same resource which makes it even harder to diagnose.

However, the first url is an absolute path and the second is relative which could be causing issues with your ALLOWED_HOSTS setting which could be generating the 400 error.

so try this:

 $.ajax({
    url: '../../api/v1/adresponses/1',
    contentType: 'application/json',
    type: 'GET', 
    success: function(data, textStatus, jqXHR) {
         console.log(data);
         }
    });

YES, without a success function you will not see any results:

success: function(data, textStatus, jqXHR) {
    console.log(data);
}

that console.log is what makes you see a result in your console.

Your origanal code does not provide:

contentType: 'application/json',

You have:

accepts: 'application/json',

OTHER TIPS

I think the problem is that the use of "/" at end of the url is mandatory. You should try with url: 'url/to/site/contacts/api/v1/adresponses/1/',

when you visiting url/to/site/contacts/api/v1/adresponses/1 it redirect to url/to/site/contacts/api/v1/adresponses/1/, depending of your tastypie configuration

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top