如何将json值发布到服务器API?

时间:2014-03-03 15:18:59

标签: javascript json

我一直试图将json值发布到API,代码如下。 也许我应该使用另一种方法将json值发送/发布到API。 库尔斯有人给我一个暗示吗? 我想能够在API上发布新的用户名和密码(用户名= bruno& password = login),怎么可能这样做? 我只能使用javascript吗?

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);

2 个答案:

答案 0 :(得分:0)

它必须是java吗? PHP可以做得很好(在我看来),如果它是第三方API,他们可能有关于如何设置它的文档。

答案 1 :(得分:0)

你是说这个?

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

这是我可以帮助的。