2012-01-04 45 views
1

我正在與我的Titanium Appcelerator項目(使用oAuth 2)中的Web服務進行交互,並且需要發出HTTP POST請求。我成功地用這樣的捲曲命令:如何通過Titanium Appcelerator發送此捲曲查詢

curl https://api.website.com/oauth/ -i -d "param=value&nextparam=goeshere" -H "Authorization: Basic hashgoeshere" 

這是行得通的。但是,當我在Appcelerator中嘗試它時,沒有任何參數成功傳遞,並且因爲我沒有得到這個工作,所以我不確定是否正確傳遞標題。我的鈦代碼如下:

xhr.validatesSecureCertificate = true; 
xhr.open("POST","https://api.website.com/oauth"); 

xhr.setTimeout(10000); 

xhr.setRequestHeader('Authorization', "Basic hashgoeshere"); 

//unsure whether I need the next line or not. I don't in curl 
//xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
xhr.send('{"param":"value","nextparam":"goeshere"}'); 

我已經調試與API服務器,我越來越如果我沒有發送參數,我會得到錯誤,所以很明顯我做錯了。我只是不知道是什麼。任何幫助將apprecaited!

回答

2

xhr.send命令應採用JavaScript對象作爲參數,而不是字符串:

xhr.send({ param :"value", nextparam :"goeshere"}); 
+0

謝謝!我不知道。你看到有類似代碼的其他問題嗎?它仍然沒有正確發送,即使我做出了改變,我也得到了同樣的迴應。 – swickblade 2012-01-05 03:09:47

+0

其他一切看起來都不錯。也可以嘗試xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');你也可以自己創建POST字符串,看看是否有幫助:xhr.send('param = value&nextparam = gohere'); – 2012-01-05 03:49:04

+0

我的版本沒有更新出於某種原因,所以我沒有看到更新。我修正了這個問題,現在這個工作。謝謝! – swickblade 2012-01-05 05:38:13