How to access XMLHttpRequest values outside the onreadystatechange function [duplicate]2019 Community Moderator ElectionHow do I return the response from an asynchronous call?How do I detect a click outside an element?How to execute a JavaScript function when I have its name as a stringHow do you access the matched groups in a JavaScript regular expression?Set a default parameter value for a JavaScript functionHow can I get query string values in JavaScript?How to get the value from the GET parameters?How can I add a key/value pair to a JavaScript object?XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-OriginHow does Trello access the user's clipboard?How to access the correct `this` inside a callback?

Is there such a thing in math the inverse of a sequence?

Can you run a ground wire from stove directly to ground pole in the ground

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

What does "rhumatis" mean?

How does a sound wave propagate?

School performs periodic password audits. Is my password compromised?

Was it really inappropriate to write a pull request for the company I interviewed with?

PTIJ: Aliyot for the deceased

Ultrafilters as a double dual

Is every open circuit a capacitor?

What is the oldest European royal house?

Problems with rounding giving too many digits

Is being socially reclusive okay for a graduate student?

Is "cogitate" an appropriate word for this?

Error in TransformedField

How can I be pwned if I'm not registered on the compromised site?

Naming Characters after Friends/Family

Why is my explanation wrong?

Should we avoid writing fiction about historical events without extensive research?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

Are there other characters in the Star Wars universe who had damaged bodies and needed to wear an outfit like Darth Vader?

Professor forcing me to attend a conference

Why aren't there more gauls like Obelix?

In the world of The Matrix, what is "popping"?



How to access XMLHttpRequest values outside the onreadystatechange function [duplicate]



2019 Community Moderator ElectionHow do I return the response from an asynchronous call?How do I detect a click outside an element?How to execute a JavaScript function when I have its name as a stringHow do you access the matched groups in a JavaScript regular expression?Set a default parameter value for a JavaScript functionHow can I get query string values in JavaScript?How to get the value from the GET parameters?How can I add a key/value pair to a JavaScript object?XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-OriginHow does Trello access the user's clipboard?How to access the correct `this` inside a callback?










0
















This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



i have a .php file that outputs a json data, which it does perfectly and i get this results:



[

"name": "ADMINISTRATOR",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "GUEST",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "KRBTGT",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "DIMERS",
"email": "",
"first_name": "Dimers",
"last_name": "David"

]


i also have a .js file which calls this results using XMLHttpRequest like so:



function loadDoc() 
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);

for (var i = 0; i < contact.length; i++)

var contacts = name: contact[i].name, email: contact[i].email,
Govphone: contact[i].first_name, phone: contact[i].last_name;

console.log(contacts);





;
xhttp.open("GET", "js/contalist.php", true);

xhttp.send();



loadDoc();


in console i'm able to get the contacts. but i need to assign the value from the response to a variable outside of the call, like so



 .factory('ContactService', [function () 
var factory = ;

factory.getContacts = function (response)

var contactsList=contacts;
return contactsList;

;

return factory;
]);


Can someone help me on how I can at least extract the contents in the contacts variable so that i can use it else where within the code?










share|improve this question









New contributor




Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Please add your output for this.responseText to the question.

    – Olayinka O
    yesterday
















0
















This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



i have a .php file that outputs a json data, which it does perfectly and i get this results:



[

"name": "ADMINISTRATOR",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "GUEST",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "KRBTGT",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "DIMERS",
"email": "",
"first_name": "Dimers",
"last_name": "David"

]


i also have a .js file which calls this results using XMLHttpRequest like so:



function loadDoc() 
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);

for (var i = 0; i < contact.length; i++)

var contacts = name: contact[i].name, email: contact[i].email,
Govphone: contact[i].first_name, phone: contact[i].last_name;

console.log(contacts);





;
xhttp.open("GET", "js/contalist.php", true);

xhttp.send();



loadDoc();


in console i'm able to get the contacts. but i need to assign the value from the response to a variable outside of the call, like so



 .factory('ContactService', [function () 
var factory = ;

factory.getContacts = function (response)

var contactsList=contacts;
return contactsList;

;

return factory;
]);


Can someone help me on how I can at least extract the contents in the contacts variable so that i can use it else where within the code?










share|improve this question









New contributor




Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • Please add your output for this.responseText to the question.

    – Olayinka O
    yesterday














0












0








0









This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



i have a .php file that outputs a json data, which it does perfectly and i get this results:



[

"name": "ADMINISTRATOR",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "GUEST",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "KRBTGT",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "DIMERS",
"email": "",
"first_name": "Dimers",
"last_name": "David"

]


i also have a .js file which calls this results using XMLHttpRequest like so:



function loadDoc() 
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);

for (var i = 0; i < contact.length; i++)

var contacts = name: contact[i].name, email: contact[i].email,
Govphone: contact[i].first_name, phone: contact[i].last_name;

console.log(contacts);





;
xhttp.open("GET", "js/contalist.php", true);

xhttp.send();



loadDoc();


in console i'm able to get the contacts. but i need to assign the value from the response to a variable outside of the call, like so



 .factory('ContactService', [function () 
var factory = ;

factory.getContacts = function (response)

var contactsList=contacts;
return contactsList;

;

return factory;
]);


Can someone help me on how I can at least extract the contents in the contacts variable so that i can use it else where within the code?










share|improve this question









New contributor




Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers



i have a .php file that outputs a json data, which it does perfectly and i get this results:



[

"name": "ADMINISTRATOR",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "GUEST",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "KRBTGT",
"email": "",
"first_name": "",
"last_name": ""
,

"name": "DIMERS",
"email": "",
"first_name": "Dimers",
"last_name": "David"

]


i also have a .js file which calls this results using XMLHttpRequest like so:



function loadDoc() 
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);

for (var i = 0; i < contact.length; i++)

var contacts = name: contact[i].name, email: contact[i].email,
Govphone: contact[i].first_name, phone: contact[i].last_name;

console.log(contacts);





;
xhttp.open("GET", "js/contalist.php", true);

xhttp.send();



loadDoc();


in console i'm able to get the contacts. but i need to assign the value from the response to a variable outside of the call, like so



 .factory('ContactService', [function () 
var factory = ;

factory.getContacts = function (response)

var contactsList=contacts;
return contactsList;

;

return factory;
]);


Can someone help me on how I can at least extract the contents in the contacts variable so that i can use it else where within the code?





This question already has an answer here:



  • How do I return the response from an asynchronous call?

    33 answers







javascript json xmlhttprequest var scoping






share|improve this question









New contributor




Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Olayinka O

611320




611320






New contributor




Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Dimers DavidDimers David

32




32




New contributor




Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Dimers David is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Quentin javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Please add your output for this.responseText to the question.

    – Olayinka O
    yesterday


















  • Please add your output for this.responseText to the question.

    – Olayinka O
    yesterday

















Please add your output for this.responseText to the question.

– Olayinka O
yesterday






Please add your output for this.responseText to the question.

– Olayinka O
yesterday













2 Answers
2






active

oldest

votes


















1














You can push the responseText and have it available elsewhere in the scope.



var theContacts=[];
function loadDoc()
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)
theContacts.push(this.responseText);

xhttp.open("GET", "js/contalist.php", true);
xhttp.send();



loadDoc();


.factory('ContactService', [function()
var factory = ;

factory.getContacts = function(response)

var contactsList=theContacts;
return contactsList;
;

return factory;
]);


To get a flattened array:



theContacts.flat();





share|improve this answer

























  • console.log(thecontacts);

    – Dimers David
    yesterday











  • @DimersDavid see update.

    – Olayinka O
    yesterday











  • Thanks a lot, this one works for me so far, but the output comes with an extra pair of "[]" how do i remove them?

    – Dimers David
    yesterday











  • I've tried that, and i get no results. and when i try to console out the pushed data i get an Uncaught TypeError: Cannot read property 'push' of undefined at XMLHttpRequest.xhttp.onreadystatechange

    – Dimers David
    yesterday











  • @DimersDavid My bad, ignore that try console.log(theContacts.flat())

    – Olayinka O
    yesterday


















0














Use a global function for your factory.getContacts, and since it's a global function, you can use it in your onreadystatechange.



var getContacts = function(contacts) 
// do whatever you want with contacts which is array


// in your factory service
.factory('ContactService', [function ()
var factory = ;

factory.getContacts = getContacts;

return factory;
]);

// in your XHR request
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);
var contacts = [] // init your array to store contacts
for (var i = 0; i < contact.length; i++)
// push the contact to array
contacts.push(name: contact[i].name, email: contact[i].email, Govphone: contact[i].first_name, phone: contact[i].last_name);


getContacts(contacts) // call the same getContacts function


;





share|improve this answer

























  • i tried this but i'm getting an error "contacts is not defined".

    – Dimers David
    yesterday











  • where did that error occur? You should check if you have defined the variable in the scope or if the variable name is correct.

    – Juky
    yesterday











  • i got it working by using 'push', given by @Olayinka. however, the output comes with an extra pair of "[]" any ideas on how i can remove them?

    – Dimers David
    yesterday











  • Because your responseText is an array hence when you push to theContacts which is also an array, you get nested array. To skip the extra [], just reuse your code that parse the responseText to json, then use concat instead of push

    – Juky
    22 hours ago

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can push the responseText and have it available elsewhere in the scope.



var theContacts=[];
function loadDoc()
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)
theContacts.push(this.responseText);

xhttp.open("GET", "js/contalist.php", true);
xhttp.send();



loadDoc();


.factory('ContactService', [function()
var factory = ;

factory.getContacts = function(response)

var contactsList=theContacts;
return contactsList;
;

return factory;
]);


To get a flattened array:



theContacts.flat();





share|improve this answer

























  • console.log(thecontacts);

    – Dimers David
    yesterday











  • @DimersDavid see update.

    – Olayinka O
    yesterday











  • Thanks a lot, this one works for me so far, but the output comes with an extra pair of "[]" how do i remove them?

    – Dimers David
    yesterday











  • I've tried that, and i get no results. and when i try to console out the pushed data i get an Uncaught TypeError: Cannot read property 'push' of undefined at XMLHttpRequest.xhttp.onreadystatechange

    – Dimers David
    yesterday











  • @DimersDavid My bad, ignore that try console.log(theContacts.flat())

    – Olayinka O
    yesterday















1














You can push the responseText and have it available elsewhere in the scope.



var theContacts=[];
function loadDoc()
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)
theContacts.push(this.responseText);

xhttp.open("GET", "js/contalist.php", true);
xhttp.send();



loadDoc();


.factory('ContactService', [function()
var factory = ;

factory.getContacts = function(response)

var contactsList=theContacts;
return contactsList;
;

return factory;
]);


To get a flattened array:



theContacts.flat();





share|improve this answer

























  • console.log(thecontacts);

    – Dimers David
    yesterday











  • @DimersDavid see update.

    – Olayinka O
    yesterday











  • Thanks a lot, this one works for me so far, but the output comes with an extra pair of "[]" how do i remove them?

    – Dimers David
    yesterday











  • I've tried that, and i get no results. and when i try to console out the pushed data i get an Uncaught TypeError: Cannot read property 'push' of undefined at XMLHttpRequest.xhttp.onreadystatechange

    – Dimers David
    yesterday











  • @DimersDavid My bad, ignore that try console.log(theContacts.flat())

    – Olayinka O
    yesterday













1












1








1







You can push the responseText and have it available elsewhere in the scope.



var theContacts=[];
function loadDoc()
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)
theContacts.push(this.responseText);

xhttp.open("GET", "js/contalist.php", true);
xhttp.send();



loadDoc();


.factory('ContactService', [function()
var factory = ;

factory.getContacts = function(response)

var contactsList=theContacts;
return contactsList;
;

return factory;
]);


To get a flattened array:



theContacts.flat();





share|improve this answer















You can push the responseText and have it available elsewhere in the scope.



var theContacts=[];
function loadDoc()
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)
theContacts.push(this.responseText);

xhttp.open("GET", "js/contalist.php", true);
xhttp.send();



loadDoc();


.factory('ContactService', [function()
var factory = ;

factory.getContacts = function(response)

var contactsList=theContacts;
return contactsList;
;

return factory;
]);


To get a flattened array:



theContacts.flat();






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Olayinka OOlayinka O

611320




611320












  • console.log(thecontacts);

    – Dimers David
    yesterday











  • @DimersDavid see update.

    – Olayinka O
    yesterday











  • Thanks a lot, this one works for me so far, but the output comes with an extra pair of "[]" how do i remove them?

    – Dimers David
    yesterday











  • I've tried that, and i get no results. and when i try to console out the pushed data i get an Uncaught TypeError: Cannot read property 'push' of undefined at XMLHttpRequest.xhttp.onreadystatechange

    – Dimers David
    yesterday











  • @DimersDavid My bad, ignore that try console.log(theContacts.flat())

    – Olayinka O
    yesterday

















  • console.log(thecontacts);

    – Dimers David
    yesterday











  • @DimersDavid see update.

    – Olayinka O
    yesterday











  • Thanks a lot, this one works for me so far, but the output comes with an extra pair of "[]" how do i remove them?

    – Dimers David
    yesterday











  • I've tried that, and i get no results. and when i try to console out the pushed data i get an Uncaught TypeError: Cannot read property 'push' of undefined at XMLHttpRequest.xhttp.onreadystatechange

    – Dimers David
    yesterday











  • @DimersDavid My bad, ignore that try console.log(theContacts.flat())

    – Olayinka O
    yesterday
















console.log(thecontacts);

– Dimers David
yesterday





console.log(thecontacts);

– Dimers David
yesterday













@DimersDavid see update.

– Olayinka O
yesterday





@DimersDavid see update.

– Olayinka O
yesterday













Thanks a lot, this one works for me so far, but the output comes with an extra pair of "[]" how do i remove them?

– Dimers David
yesterday





Thanks a lot, this one works for me so far, but the output comes with an extra pair of "[]" how do i remove them?

– Dimers David
yesterday













I've tried that, and i get no results. and when i try to console out the pushed data i get an Uncaught TypeError: Cannot read property 'push' of undefined at XMLHttpRequest.xhttp.onreadystatechange

– Dimers David
yesterday





I've tried that, and i get no results. and when i try to console out the pushed data i get an Uncaught TypeError: Cannot read property 'push' of undefined at XMLHttpRequest.xhttp.onreadystatechange

– Dimers David
yesterday













@DimersDavid My bad, ignore that try console.log(theContacts.flat())

– Olayinka O
yesterday





@DimersDavid My bad, ignore that try console.log(theContacts.flat())

– Olayinka O
yesterday













0














Use a global function for your factory.getContacts, and since it's a global function, you can use it in your onreadystatechange.



var getContacts = function(contacts) 
// do whatever you want with contacts which is array


// in your factory service
.factory('ContactService', [function ()
var factory = ;

factory.getContacts = getContacts;

return factory;
]);

// in your XHR request
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);
var contacts = [] // init your array to store contacts
for (var i = 0; i < contact.length; i++)
// push the contact to array
contacts.push(name: contact[i].name, email: contact[i].email, Govphone: contact[i].first_name, phone: contact[i].last_name);


getContacts(contacts) // call the same getContacts function


;





share|improve this answer

























  • i tried this but i'm getting an error "contacts is not defined".

    – Dimers David
    yesterday











  • where did that error occur? You should check if you have defined the variable in the scope or if the variable name is correct.

    – Juky
    yesterday











  • i got it working by using 'push', given by @Olayinka. however, the output comes with an extra pair of "[]" any ideas on how i can remove them?

    – Dimers David
    yesterday











  • Because your responseText is an array hence when you push to theContacts which is also an array, you get nested array. To skip the extra [], just reuse your code that parse the responseText to json, then use concat instead of push

    – Juky
    22 hours ago















0














Use a global function for your factory.getContacts, and since it's a global function, you can use it in your onreadystatechange.



var getContacts = function(contacts) 
// do whatever you want with contacts which is array


// in your factory service
.factory('ContactService', [function ()
var factory = ;

factory.getContacts = getContacts;

return factory;
]);

// in your XHR request
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);
var contacts = [] // init your array to store contacts
for (var i = 0; i < contact.length; i++)
// push the contact to array
contacts.push(name: contact[i].name, email: contact[i].email, Govphone: contact[i].first_name, phone: contact[i].last_name);


getContacts(contacts) // call the same getContacts function


;





share|improve this answer

























  • i tried this but i'm getting an error "contacts is not defined".

    – Dimers David
    yesterday











  • where did that error occur? You should check if you have defined the variable in the scope or if the variable name is correct.

    – Juky
    yesterday











  • i got it working by using 'push', given by @Olayinka. however, the output comes with an extra pair of "[]" any ideas on how i can remove them?

    – Dimers David
    yesterday











  • Because your responseText is an array hence when you push to theContacts which is also an array, you get nested array. To skip the extra [], just reuse your code that parse the responseText to json, then use concat instead of push

    – Juky
    22 hours ago













0












0








0







Use a global function for your factory.getContacts, and since it's a global function, you can use it in your onreadystatechange.



var getContacts = function(contacts) 
// do whatever you want with contacts which is array


// in your factory service
.factory('ContactService', [function ()
var factory = ;

factory.getContacts = getContacts;

return factory;
]);

// in your XHR request
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);
var contacts = [] // init your array to store contacts
for (var i = 0; i < contact.length; i++)
// push the contact to array
contacts.push(name: contact[i].name, email: contact[i].email, Govphone: contact[i].first_name, phone: contact[i].last_name);


getContacts(contacts) // call the same getContacts function


;





share|improve this answer















Use a global function for your factory.getContacts, and since it's a global function, you can use it in your onreadystatechange.



var getContacts = function(contacts) 
// do whatever you want with contacts which is array


// in your factory service
.factory('ContactService', [function ()
var factory = ;

factory.getContacts = getContacts;

return factory;
]);

// in your XHR request
xhttp.onreadystatechange = function()
if (this.readyState == 4 && this.status == 200)


var contact = JSON.parse(this.responseText);
var contacts = [] // init your array to store contacts
for (var i = 0; i < contact.length; i++)
// push the contact to array
contacts.push(name: contact[i].name, email: contact[i].email, Govphone: contact[i].first_name, phone: contact[i].last_name);


getContacts(contacts) // call the same getContacts function


;






share|improve this answer














share|improve this answer



share|improve this answer








edited 22 hours ago

























answered yesterday









JukyJuky

1879




1879












  • i tried this but i'm getting an error "contacts is not defined".

    – Dimers David
    yesterday











  • where did that error occur? You should check if you have defined the variable in the scope or if the variable name is correct.

    – Juky
    yesterday











  • i got it working by using 'push', given by @Olayinka. however, the output comes with an extra pair of "[]" any ideas on how i can remove them?

    – Dimers David
    yesterday











  • Because your responseText is an array hence when you push to theContacts which is also an array, you get nested array. To skip the extra [], just reuse your code that parse the responseText to json, then use concat instead of push

    – Juky
    22 hours ago

















  • i tried this but i'm getting an error "contacts is not defined".

    – Dimers David
    yesterday











  • where did that error occur? You should check if you have defined the variable in the scope or if the variable name is correct.

    – Juky
    yesterday











  • i got it working by using 'push', given by @Olayinka. however, the output comes with an extra pair of "[]" any ideas on how i can remove them?

    – Dimers David
    yesterday











  • Because your responseText is an array hence when you push to theContacts which is also an array, you get nested array. To skip the extra [], just reuse your code that parse the responseText to json, then use concat instead of push

    – Juky
    22 hours ago
















i tried this but i'm getting an error "contacts is not defined".

– Dimers David
yesterday





i tried this but i'm getting an error "contacts is not defined".

– Dimers David
yesterday













where did that error occur? You should check if you have defined the variable in the scope or if the variable name is correct.

– Juky
yesterday





where did that error occur? You should check if you have defined the variable in the scope or if the variable name is correct.

– Juky
yesterday













i got it working by using 'push', given by @Olayinka. however, the output comes with an extra pair of "[]" any ideas on how i can remove them?

– Dimers David
yesterday





i got it working by using 'push', given by @Olayinka. however, the output comes with an extra pair of "[]" any ideas on how i can remove them?

– Dimers David
yesterday













Because your responseText is an array hence when you push to theContacts which is also an array, you get nested array. To skip the extra [], just reuse your code that parse the responseText to json, then use concat instead of push

– Juky
22 hours ago





Because your responseText is an array hence when you push to theContacts which is also an array, you get nested array. To skip the extra [], just reuse your code that parse the responseText to json, then use concat instead of push

– Juky
22 hours ago



Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко