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
  • 3
  • 15
  • 1
    stackoverflow-user Exception : http is undefined, please make an ajax call – john Smith Mar 03 '14 at 15:20
  • possible duplicate of [jQuery posting JSON](http://stackoverflow.com/questions/5570747/jquery-posting-json) – Dan Esparza Mar 03 '14 at 15:22
  • @johnSmith Ok, do you mean I should do it with a ajax call? Can you give me a hint, on how to do it? – Mr Toni WP Mar 03 '14 at 15:23
  • You didn't created your request: `var http = new XMLHttpRequest();` – Tommi Mar 03 '14 at 15:24
  • @Tommi Hi, know its a warning and a error:
    Parse error: syntax error, unexpected '=' in /var/www/html/api/v11/Json.php(34) : eval()'d code on line 1

    Warning: Invalid argument supplied for foreach() in /var/www/html/api/v11/Json.php on line 6
    – Mr Toni WP Mar 03 '14 at 15:27
  • It's a response from server side − error in php code. – Tommi Mar 03 '14 at 15:29

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