jquery - Backbone Create model with Django Rest Framework -


im trying create new model rest api using django rest framework.

this serializer:

class fooserializer(serializers.hyperlinkedmodelserializer):     class meta:         model = foo 

this view.py

@api_view(['post', 'get','delete','options']) def foos(request):     """     api endpoint create, delete , foos     """     authentication_classes = (authentication.tokenauthentication,)     permission_classes = (permissions.isauthenticated,)      model = foo     serializer_class = fooserializer      if request.method == "post":         data = jsonparser().parse(request)         serializer = fooserializer(data=data)         if serializer.is_valid():             serializer.save()             return jsonresponse(serializer.data, status=201)         else:             return jsonresponse(serializer.errors, status=400)      return response(serializer.data) 

then in backbone view:

       foo = new foo             name:'bla di bla di'         foo.save() 

noting happens except options fail, there no post.

failing options

options http://127.0.0.1:8080/api/foo/   

i dont know do, not happen if leave out contenttype:"application/json" part of post (when doing manual post)

it works curl in terminal.

in chrome inspector > network this:

request url:http://127.0.0.1:8080/api/foo/ request headersview source access-control-request-headers:accept, origin, authorization, content-type access-control-request-method:post cache-control:no-cache origin:http://localhost:8000 pragma:no-cache 

request, , response empty.

edit

i turned of server rest api , exact same thing happens! tels me 1 thing, not server. must ajax stuff.

im confused :-(

when doing cross-domain request, it's normal options call before doing post.

if options call happens, no post follows, cause of cors failure.

check server response - returning correct headers?

you need 3:

'access-control-allow-origin' 'access-control-allow-methods'
'access-control-allow-headers'

these must match request.

see answer quick fix: https://stackoverflow.com/a/3520073

or page background + full explanation : http://www.html5rocks.com/en/tutorials/cors/


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -