2014-03-03 92 views
0

我一直在試圖將json值發佈到API,代碼如下。 也許我應該使用其他方法將json值發送/發佈到API。 Couls有人給我一個提示? 我想能夠發佈一個新的用戶名和密碼到API(用戶名= bruno & password = login),怎麼可能做到這一點? 我可以只使用JavaScript嗎?如何將json值發佈到服務器API?

var url = "http://192.168.8.143/api/v11/login/"; 
var parameters = "username=bruno&password=login"; 
http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", parameters .length); 
http.setRequestHeader("Connection", "close"); 

http.onreadystatechange = function() {//Handler function for call back on state change. 
    if(http.readyState == 4) { 

     alert(http.responseText); 
    } 
} 
http.send(parameters); 
+1

stackoverflow-user例外:http是undefined,請打一個ajax調用 –

+0

[jQuery發佈JSON]的可能重複(http://stackoverflow.com/questions/5570747/jquery-posting-json) –

+0

@johnSmith好的,你的意思是我應該用ajax調用嗎?你能給我一個提示,告訴我怎麼做? –

回答

0

它是否必須是java? PHP可以做得很好(在我看來),如果它是第三方API,他們可能有如何設置它的文檔。

+0

不,它不一定是JAVA ...它必須與JavaScript,也許ajax ... –

0

你的意思是?

var parameters = '"username="+bruno+"&password="+login+"'; 

這是我可以幫助的。

+0

好吧,我用你的代碼行,但知道我得到此錯誤和警告:拒絕設置不安全標題「Content-length」plan.dev/:1 拒絕設置不安全標題「Connection」plan.dev/:1 加載資源失敗:服務器響應的狀態爲401 (未授權) –

相關問題