私はtempermonkey(フォームによるユーザー入力の読み込み)のスクリプトからdjangoにデータを転送しようとしています。 転送は機能しますが、私はPythonでデータを使用できません(私はフォーマットで何か間違っていると思います)。これはtempermonkeyで私のスクリプトの例である投稿データをPythonからdjangoに送信
:これは私のview.py
def index(request):
data=request.POST
dataTransform=(json.loads(json.dumps(request.POST)))
return HttpResponse(dataTransform)
応答である
[{"name" : "Pippo" , "surname" : "Pluto" , "address" : "street xxx" , "number" : "1234"},{"name" : "aaa" , "surname" : "bbb" , "address" : "street xxx" , "number" : "4321"},{"name" : "y" , "surname" : "x" , "address" : "street xxx" , "number" : "333"}]
:
GM_xmlhttpRequest({
method: "POST",
url: "www.example.com",
data: datasend,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
alert(response.responseText);
}
});
のdatasendは、このようなオブジェクトの配列が含まれていますは:
(u'[{"name" : "Pippo" , "surname" : "Pluto" , "address" : "street xxx" , "number" : "1234"},{"name" : "aaa" , "surname" : "bbb" , "address" : "street xxx" , "number" : "4321"},{"name" : "y" , "surname" : "x" , "address" : "street xxx" , "number" : "333"}]',u")
長さ= 1のdict。どのように各要素にアクセスできますか? 私の目標は、この情報をデータベースに保存することです。
ありがとうございました!
GM_xmlhttpRequest({
method: "POST",
url: "www.example.com",
data: JSON.stringify(datasend),
headers: {
"Content-Type": "application/json"
},
onload: function(response) {
alert(response.responseText);
}
});
をし、それを正しく受信:
を一般的に、あなたはこのためにはevalのようなものを()を使用しますが、あなたはこれを見たいと思うかもしれませんhttps://docs.python.org/2/library/ast.html#ast.literal_eval – Natecat