Why can't separate the function for xmlHttp.onreadystatechange?












0















The below js file test.js` works fine in my html.



function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


Now i want to separate them



        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
alert(xmlHttp.responseText);


in a single function.



I rewrite the test1.js as test2.js.



var xmlHttp;
function ready(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}

function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = ready;
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


The test2.js encounter error info:



test2.js:4 Uncaught TypeError: Cannot read property 'readyState' of undefined
at XMLHttpRequest.ready (test2.js:4)


Another issue :what is the right order for the following statements?

I have seen some material write them as below :



    xmlHttp.open("post", "test.php",true); 
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){ }


Other material also seen:



    xmlHttp.onreadystatechange =  function(){  }
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);


And other order in webpage xmlHttp statements order



xmlhttp.open("POST", "Demo", true);
xmlhttp.onreadystatechange=myCallBack;
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("FirstName=Nat&LastName=Dunn");









share|improve this question

























  • Just pass xmlHttp as a parameter...

    – ritaj
    Nov 24 '18 at 11:38











  • I have defined xmlHttp as a global function,no need to pass xmlHttp as a parameter.

    – scrapy
    Nov 24 '18 at 11:53
















0















The below js file test.js` works fine in my html.



function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


Now i want to separate them



        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
alert(xmlHttp.responseText);


in a single function.



I rewrite the test1.js as test2.js.



var xmlHttp;
function ready(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}

function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = ready;
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


The test2.js encounter error info:



test2.js:4 Uncaught TypeError: Cannot read property 'readyState' of undefined
at XMLHttpRequest.ready (test2.js:4)


Another issue :what is the right order for the following statements?

I have seen some material write them as below :



    xmlHttp.open("post", "test.php",true); 
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){ }


Other material also seen:



    xmlHttp.onreadystatechange =  function(){  }
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);


And other order in webpage xmlHttp statements order



xmlhttp.open("POST", "Demo", true);
xmlhttp.onreadystatechange=myCallBack;
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("FirstName=Nat&LastName=Dunn");









share|improve this question

























  • Just pass xmlHttp as a parameter...

    – ritaj
    Nov 24 '18 at 11:38











  • I have defined xmlHttp as a global function,no need to pass xmlHttp as a parameter.

    – scrapy
    Nov 24 '18 at 11:53














0












0








0








The below js file test.js` works fine in my html.



function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


Now i want to separate them



        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
alert(xmlHttp.responseText);


in a single function.



I rewrite the test1.js as test2.js.



var xmlHttp;
function ready(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}

function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = ready;
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


The test2.js encounter error info:



test2.js:4 Uncaught TypeError: Cannot read property 'readyState' of undefined
at XMLHttpRequest.ready (test2.js:4)


Another issue :what is the right order for the following statements?

I have seen some material write them as below :



    xmlHttp.open("post", "test.php",true); 
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){ }


Other material also seen:



    xmlHttp.onreadystatechange =  function(){  }
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);


And other order in webpage xmlHttp statements order



xmlhttp.open("POST", "Demo", true);
xmlhttp.onreadystatechange=myCallBack;
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("FirstName=Nat&LastName=Dunn");









share|improve this question
















The below js file test.js` works fine in my html.



function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


Now i want to separate them



        if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ 
alert(xmlHttp.responseText);


in a single function.



I rewrite the test1.js as test2.js.



var xmlHttp;
function ready(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}

function sendData()
{
var formData = new FormData( document.querySelector("form") );
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);
xmlHttp.onreadystatechange = ready;
}

ob = document.getElementById("submit");
ob.addEventListener("click",sendData);


The test2.js encounter error info:



test2.js:4 Uncaught TypeError: Cannot read property 'readyState' of undefined
at XMLHttpRequest.ready (test2.js:4)


Another issue :what is the right order for the following statements?

I have seen some material write them as below :



    xmlHttp.open("post", "test.php",true); 
xmlHttp.send(formData);
xmlHttp.onreadystatechange = function(){ }


Other material also seen:



    xmlHttp.onreadystatechange =  function(){  }
xmlHttp.open("post", "test.php",true);
xmlHttp.send(formData);


And other order in webpage xmlHttp statements order



xmlhttp.open("POST", "Demo", true);
xmlhttp.onreadystatechange=myCallBack;
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
xmlhttp.send("FirstName=Nat&LastName=Dunn");






javascript ajax onreadystatechange






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 11:52







scrapy

















asked Nov 24 '18 at 11:35









scrapyscrapy

193219




193219













  • Just pass xmlHttp as a parameter...

    – ritaj
    Nov 24 '18 at 11:38











  • I have defined xmlHttp as a global function,no need to pass xmlHttp as a parameter.

    – scrapy
    Nov 24 '18 at 11:53



















  • Just pass xmlHttp as a parameter...

    – ritaj
    Nov 24 '18 at 11:38











  • I have defined xmlHttp as a global function,no need to pass xmlHttp as a parameter.

    – scrapy
    Nov 24 '18 at 11:53

















Just pass xmlHttp as a parameter...

– ritaj
Nov 24 '18 at 11:38





Just pass xmlHttp as a parameter...

– ritaj
Nov 24 '18 at 11:38













I have defined xmlHttp as a global function,no need to pass xmlHttp as a parameter.

– scrapy
Nov 24 '18 at 11:53





I have defined xmlHttp as a global function,no need to pass xmlHttp as a parameter.

– scrapy
Nov 24 '18 at 11:53












1 Answer
1






active

oldest

votes


















1














In sendData you have:



var xmlHttp = new XMLHttpRequest();


Your only mistake is including the var here - just do this instead:



xmlHttp = new XMLHttpRequest();


The reason this matters is that the var is declaring a new local variable of the same name, which is then getting assigned to - so ready doesn't get access to it. It accesses the global xmlHttp variable, which is never assigned to. By removing the var as shown above, you ensure that the global variable is assigned to - and this should work. (Although of course it's not best practice to use globals.)






share|improve this answer
























  • Another issue :what is the right order for the following statements?

    – scrapy
    Nov 24 '18 at 12:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457706%2fwhy-cant-separate-the-function-for-xmlhttp-onreadystatechange%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In sendData you have:



var xmlHttp = new XMLHttpRequest();


Your only mistake is including the var here - just do this instead:



xmlHttp = new XMLHttpRequest();


The reason this matters is that the var is declaring a new local variable of the same name, which is then getting assigned to - so ready doesn't get access to it. It accesses the global xmlHttp variable, which is never assigned to. By removing the var as shown above, you ensure that the global variable is assigned to - and this should work. (Although of course it's not best practice to use globals.)






share|improve this answer
























  • Another issue :what is the right order for the following statements?

    – scrapy
    Nov 24 '18 at 12:06
















1














In sendData you have:



var xmlHttp = new XMLHttpRequest();


Your only mistake is including the var here - just do this instead:



xmlHttp = new XMLHttpRequest();


The reason this matters is that the var is declaring a new local variable of the same name, which is then getting assigned to - so ready doesn't get access to it. It accesses the global xmlHttp variable, which is never assigned to. By removing the var as shown above, you ensure that the global variable is assigned to - and this should work. (Although of course it's not best practice to use globals.)






share|improve this answer
























  • Another issue :what is the right order for the following statements?

    – scrapy
    Nov 24 '18 at 12:06














1












1








1







In sendData you have:



var xmlHttp = new XMLHttpRequest();


Your only mistake is including the var here - just do this instead:



xmlHttp = new XMLHttpRequest();


The reason this matters is that the var is declaring a new local variable of the same name, which is then getting assigned to - so ready doesn't get access to it. It accesses the global xmlHttp variable, which is never assigned to. By removing the var as shown above, you ensure that the global variable is assigned to - and this should work. (Although of course it's not best practice to use globals.)






share|improve this answer













In sendData you have:



var xmlHttp = new XMLHttpRequest();


Your only mistake is including the var here - just do this instead:



xmlHttp = new XMLHttpRequest();


The reason this matters is that the var is declaring a new local variable of the same name, which is then getting assigned to - so ready doesn't get access to it. It accesses the global xmlHttp variable, which is never assigned to. By removing the var as shown above, you ensure that the global variable is assigned to - and this should work. (Although of course it's not best practice to use globals.)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 11:59









Robin ZigmondRobin Zigmond

2,6941611




2,6941611













  • Another issue :what is the right order for the following statements?

    – scrapy
    Nov 24 '18 at 12:06



















  • Another issue :what is the right order for the following statements?

    – scrapy
    Nov 24 '18 at 12:06

















Another issue :what is the right order for the following statements?

– scrapy
Nov 24 '18 at 12:06





Another issue :what is the right order for the following statements?

– scrapy
Nov 24 '18 at 12:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457706%2fwhy-cant-separate-the-function-for-xmlhttp-onreadystatechange%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Marschland

Redirect URL with Chrome Remote Debugging Android Devices