JSONデータの送信部分で悩んでおります。 どうしても解決できないので質問させていただきたく思います。
まず、Djangoを使用せずに普通のHTMLファイルにてjQueryのテストをしてみました。 ((ajaxTest.html)) <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script> <script type="text/javascript"> $(function(){ $.getJSON("./aaa.json", function(data){ console.debug(data); $.each(data, function(){ $.each(this.fields, function(key, value){ console.log(key + ":" + value); //firebug用 }); }); }); }); </script> aaa.jsonの中身はこちら <http://ideone.com/zfhjJe>。 Firebugによるconsoleの結果はこちら <http://ideone.com/An3UrP>。 この状態では上手く作動しているようですが、これと同じことをDjangoを使って行った場合に全く異なった挙動になります。 ((models.py)) from django.db import models class nikkeihyou(models.Model): taishaku = models.PositiveSmallIntegerField() kamoku = models.CharField(max_length=15, unique=True) kamoku_hira = models.CharField(max_length=25, unique=True, blank=True) kamoku_alpha = models.CharField(max_length=25, unique=True, blank=True) ((views.py)) import json from django.core import serializers def torihiki(request): from account.forms import Torihiki form = Torihiki(request.POST) view = { 'form': form, } return render_to_response('account/torihiki.html', view, context_instance=RequestContext(request)) def ajax(request): from account.models import nikkeihyou rows = nikkeihyou.objects.using('actest').all().order_by('id') data = serializers.serialize('json', rows) return HttpResponse(json.dumps(data), content_type="application/json") ((torihiki.html)) <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(function() { $.getJSON('http://localhost:8080/account/ajax/', function(data) { console.debug(data); $.each(data, function(){ */*** エラー発生 ***/* $.each(this.fields, function(key, value){ console.log(key + ":" + value); }); }); }); }); </script> Firebugが出力したエラー内容はこちら <http://ideone.com/9zOUVy>。 一見、console.debug(data)の内容はaaa.jsonと同じようですが、上手くいっているajaxTest.htmlの方はconsole.debug(data)の部分の出力がObjectなど書かれていて異なっています。 自分がviews.pyにて書いたserializeの部分が問題なのかなと思ったのですが。 それかJavaScript側でのデコードが足りないとか。 どのように書き換えたら良いのでしょうか? アドバイス頂けましたらありがたいです。 宜しくお願い致します。 -- -- ----------------- http://djangoproject.jp/ ----------------- You received this message because you are subscribed to the Google Groups "django-ja" group. To post to this group, send email to django-ja@googlegroups.com To unsubscribe from this group, send email to django-ja-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-ja --- このメールは Google グループのグループ「django-ja」の登録者に送られています。 このグループから退会し、グループからのメールの配信を停止するには django-ja+unsubscr...@googlegroups.com にメールを送信してください。 その他のオプションについては、https://groups.google.com/d/optout にアクセスしてください。
