using get method fetching html data, getting wrong response result

124 views Asked by At

I am using jQuery get method to load html's data to store in to my array, for that i use this method:

var temps = function(views){
    var tempts = [];
    $.each(views, function(i,view){
    if(view){
        tempts.push($.get("templates/" + view + ".html"), function(data){
        console.log(data);//i am not getting response text.. getting array object.
    })
}

})

while i console the data i am getting a array object as like this:

[Object { readyState= 4,  responseText="<div id="login">\n  <form...eldset>\n   </form>\n</div>",  status=200,  more...}, function()]

what is wrong here... how to get response text directly..?

2

There are 2 answers

0
TGH On BEST ANSWER

Looks like the callback is defined outside the $.get call

Try:

tempts.push($.get("templates/" + view + ".html", function(data){console.log(data);}));
0
Matt Browne On

In your code you're not actually handling the response at all...you're just sending the GET request out into thin air.

In order to handle the response you need to pass a callback function to $.get (called the "success" callback because it's the function that's run if the request returns successfully):

$.get("templates/" + view + ".html", function(data) {
    //do something with data
});