2012-05-10 7 views
1

GET、POST、PUT、DELETEメソッドを使用してdjango-tastypieを使用しましたが、クライアントとサーバーの両方が同じドメインになってもスムーズに動作しますが、起こった。クロスドメインjsonデータがdjango tastypieで拒否されました

ヒント?

models.py

from django.db import models 
class Entry(models.Model): 
    title = models.CharField(max_length=30) 
    body = models.CharField(max_length=40) 
    pub_date = models.DateField() 
    slug=models.CharField(max_length=30) 

resources.py

from django.contrib.auth.models import User 
from tastypie.authorization import Authorization 
from tastypie import fields 
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS 
from myapp.models import Entry 


class UserResource(ModelResource): 
    class Meta: 
     queryset = User.objects.all() 
     resource_name = 'user' 
     excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser'] 
     filtering = { 
     'username': ALL, 
     } 


class EntryResource(ModelResource): 

    class Meta: 
    queryset = Entry.objects.all() 
    resource_name = 'entry' 
    authorization = Authorization() 
    filtering = { 
     'user': ALL_WITH_RELATIONS, 
     'pub_date': ['exact', 'lt', 'lte', 'gte', 'gt'], 
     } 

urls.py

from django.views.generic.simple import direct_to_template 
from django.conf.urls.defaults import * 
from tastypie.api import Api 
from myapp.resources import EntryResource, UserResource 

v1_api = Api(api_name='v1') 
v1_api.register(UserResource()) 
v1_api.register(EntryResource()) 

urlpatterns = patterns('', 
# The normal jazz here... 
    (r'^api/', include(v1_api.urls)), 
    (r'^basic/$', direct_to_template, {'template': 'todos/test.html'}) 
) 

とテンプレートファイルは、以下の通りである

<!DOCTYPE html> 
<html> 
    <head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script language="javascript"> 
    jQuery(document).ready(function($) { 
     var data = JSON.stringify({ 
     "body": "This will prbbly be my lst edited post.", 
     "pub_date": "2011-05-22T00:46:38", 
     "slug": "another-post", 
     "title": "Another Post", 

    }); 
    $.ajax({ 
     url: "http://localhost:8000/api/v1/entry/1/?format=json", 
     type: 'PUT', 
      contentType: 'application/json', 
     data: data, 
     dataType: 'json', 
     processData: false, 
     success:function(data) { 
      alert(data); 
      }, 
     error : function(data){ 
      alert('error') 
      }, 
     }) 
    }) 

</script> 
    </head> 
    <body> 
body content goes here 
    </body> 
</html> 

今私がhttp://localhost:8000/basicを実行してCRUDのために完璧に動作しています

後で私はapache serverをインストールし、そのbasic.htmlをコピーしました。 http://localhost:81/basic.htmlを実行すると、jsonデータはサーバーによって受け入れられません。私は、ApacheとPythonサーバーの両方を並列に実行しています。

+0

あなたは手がかりを欲しがっています。 – Tadeck

+0

編集した質問を確認してください – sumit

答えて

2

クロスドメインajaxを実行するには、クライアントとサーバーを設定する必要があります。 jqueryので

、あなたはサーバー側では、あなたの$.ajax()コール (http://api.jquery.com/jQuery.ajax/を参照)

crossDomain : trueを設定するだろう、あなたはあなたが見つけることができ受け入れるヘッダ http://enable-cors.org/

のカップルを設定する必要がありますCORS仕様を参照してクロスドメインリクエストに関する詳細をご確認ください: http://www.w3.org/TR/cors/

+0

djangoでヘッダを受け入れる場所を教えてください。 – sumit

+0

これはdjangoミドルウェアで行うことができます。ここには良い例があります:https://gist.github.com/1369619 – shreddd

+0

ミドルウェア層の仕組みは次の通りです - https://docs.djangoproject.com/en/dev/topics/http/middleware/ – shreddd

関連する問題