urlconfが次のようなAPIを公開しているdjangoアプリケーションがあります。
url('^links/', linkhandler),
リンクハンドラーは、POST(Create関数)を以下に示したdjangoピストンリソースです。
def create(self, request):
try:
link_obj = Link.objects.get(link = request.POST['link'])
print link_obj
link_obj.rec_count = link_obj.rec_count+ request.POST.get('rec_count', 1)
link_obj.save()
return link_obj
except:
try:
query_obj = Query.objects.get(query_word = request.POST['query'])
print query_obj
except:
query_obj = Query(query_word = request.POST['query'])
query_obj.save()
link_obj = Link(link = request.POST['link'], rec_count = request.POST.get('rec_count', 1), query = query_obj)
link_obj.save()
return link_obj
上記はすべて問題なく、CURLを介してPOSTリクエストを実行すると、完全に問題なく機能します。たとえば、以下は機能する私のCURLリクエストです。
curl -d "query=hp&link=http://www.shopping.hp.com/&rec_count=1" http://localhost:8000/api/links/
しかし、greasemonkeyスクリプトからこれを試してみると、常に400エラーが返されます:(
以下は、関連するグリースモンキースクリプトです
GM_xmlhttpRequest({
method:"POST",
url:"http://localhost:8000/api/links/",
headers:{
"User-Agent":"Mozilla/5.0",
"Accept":"text/json",
"Content-Type" : "application/x-www-form-urlencoded"
},
data: encodeURI("query="+GM_getValue('q', '')+"&link="+this.previousSibling.href+"&rec_count=1"),
onerror: function errorhand()
{
alert("error occurred!");
}
});
何が問題になる可能性がありますか?