template 部分(入力部分とかは適当に。core-ajax は実際は djang-ajax を使う。)
<paper-input-decorator label="" error="入力必須" isInvalid="{{!$.input_name.validity.valid}}"> <input is="core-input" id="input_name" required type="text" value="{{ name }}"> </paper-input-decorator> <paper-button on-click="{{ post_data }}"></paper-button> <core-ajax id="post" url="/someurl" method="post" handleAs="json" on-core-response="{{ response }}" on-core-error="{{ error }}"> </core-ajax>
script 部分
post_data: function() { if(!this.$.input_name.validity.valid){ alert("名前を入力してください"); } else { var data = { 'name': this.name, }; this.$.post.body = "data=" + JSON.stringify(data); this.$.post.go(); } }, response: function (event, response) { }, error: function(event, response) { }
受け取り側(Python Django)
class ActivityInfoListApi(View): def post(self, request, *args, **kwargs): data = request.POST.get("data", None) if data is None or len(data) == 0: return HttpResponseBadRequest() obj = json.loads(data) try: name = obj["name"] # 型チェック if (not isinstance(name, str) and not isinstance(name, unicode)): return HttpResponseBadRequest() # 受け取った値で何か処理… return HttpResponse("json形式文字列…") except KeyError: logging.error("Missing Parameter") return HttpResponseBadRequest()
0 件のコメント:
コメントを投稿