最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论