-6

I started with this post some time ago, unfortunately it didn't work. I decided to look in the console, an found out that it was not sending the requests' headers as they were unsafe. So I decided to comment them out for now.

However there is one more problem: "http is not defined". How do I solve that?

// will use this to turn an object into a url encoded string
var serializeObject = function(obj) {
  var output = '';
  for(var attr in obj) {
    if(obj.hasOwnProperty(attr)) {
      output += attr + '=' + obj + '&';
    }
  }
  return output.slice(0, -1);
};
var url = 'http://spacej.ru/sample/getMcoordinates.php';
// you need to serialize your data into key value pairs like the following
var exampleCoords = {
  x: 31,
  y: 74,
  z: 28
};
// postData will be x=10&y=20&z=30
var postData = serializeObject(exampleCoords);

var request = new XMLHttpRequest();
request.open('POST', url, true);
/*
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", postData.length);
request.setRequestHeader("Connection", "close");

*/

// this function gets called when the request changes

  // mistake pops up here ! 

    http.onreadystatechange = function() {
    // request was successful
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }

}


http.send(postData);
Community
  • 1
  • 1
  • 2
    Well, http is not defined means that http is not defined. You only seemed to have renamed half of your variables, or simply have no clue what you are doing and just copy/pasted something, guessing it would magically work. – Sumurai8 Aug 07 '15 at 17:26
  • Actually he just copy/pasted the code answered in his previous question, that had this bug. – Hacketo Aug 07 '15 at 17:30

1 Answers1

3

Try renaming the http variable to request. The http variable does not appear to be declared anywhere in your code.

TwitchBronBron
  • 2,783
  • 3
  • 21
  • 45