0

I been trying to post json values to an API, with the code bellow. Maybe I should use another approach to send/post json values to an API. Couls somebody give me a hint? I wanna be able to post a new username and password to the API (username=bruno&password=login), how is it possible to do that? Can I do It only using 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);
Mr Toni WP
  • 191
  • 2
  • 15

2 Answers2

0

Does it have to be java? PHP can do it nicely ( in my opinion ) also if it is a third party api they probably have documentation on how to set it up.

Martin E.
  • 267
  • 1
  • 12
0

You mean this?

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

This is something that I can help.

Aydomir
  • 21
  • 6
  • Ok, I used your line of code, but know i get this error and warning: Refused to set unsafe header "Content-length" plan.dev/:1 Refused to set unsafe header "Connection" plan.dev/:1 Failed to load resource: the server responded with a status of 401 (Unauthorized) – Mr Toni WP Mar 03 '14 at 15:52