javascript - How to deal with "Uncaught ReferenceError: http is not defined" error? - Stack Overflow

I started with this post some time ago, unfortunately it didn't work. I decided to look in the con

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 ment 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 = '.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);

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 ment 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);
Share Improve this question edited May 23, 2017 at 12:23 CommunityBot 11 silver badge asked Aug 7, 2015 at 17:22 Алексей НаумовАлексей Наумов 1891 gold badge3 silver badges15 bronze badges 2
  • 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 Commented Aug 7, 2015 at 17:26
  • Actually he just copy/pasted the code answered in his previous question, that had this bug. – Hacketo Commented Aug 7, 2015 at 17:30
Add a ment  | 

1 Answer 1

Reset to default 3

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745178535a4615311.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信