0
我有的露出其URL的conf這個樣子的一個API Django應用程序,Greasemonkey的POST請求始終返回400錯誤
url('^links/', linkhandler),
的鏈路處理器是一個Django活塞資源,其POST(創建函數)下面我給了,所有上述
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
是罰款,當我做POST請求throught捲曲,它完美的罰款。 例如,下面就是我的要求捲曲其中工程,
curl -d "query=hp&link=http://www.shopping.hp.com/&rec_count=1" http://localhost:8000/api/links/
但是當我嘗試這從Greasemonkey的腳本,它總是給回一個400錯誤:(
下面是相關的Greasemonkey腳本
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!");
}
});
可能是什麼問題?
你是否檢查過'data'參數是否正確生成?並且通過查看服務器日誌,請求url在curl和geasemonkey版本之間看起來相同? – 2011-03-15 20:09:52
您也可以在Wireshark中查看這兩者。我注意到'Accept'標題是不同的。另一種方法是將cURL硬編碼的URL複製到腳本中作爲測試。 – 2011-03-15 20:13:48