-1

I am making the following jquery ajax call to a codeigniter php function:

    var html ="";

    $.ajax({
                        type:"POST",
                        url: "Ajax/getHtml",
                        data: { u : contents },
                        dataType: 'html',       
                        success: function(data) {
                            html = data;
                        },

                        error: function(jqXHR, textStatus, errorThrown) {
                                console.log('error');
                                console.log(jqXHR,textStatus, errorThrown);
                        }
                    });

                     console.log('html', html);

This is working correctly and html is being returned on success which I can see if I log 'data' to the console. However I don't seem to be able to capture the HTML response in a javascript variable. I have declared one (html) globally. When I look at the console I see:

html 
jquery-2.1.1.js:8623 XHR finished loading: POST "http://localhost/b1/Ajax/getHtml".

I'm not experienced with javascript but this seems out of order, so I think the problem is that javascripts asynchronous nature causes the script to move forward without waiting for the ajax request to return results.

Does this make sense and how should I fix this?

user1592380
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

0

I'm afraid it is not possible.

If you want to do something with the html data returned from the ajax, you should do it inside the success function, or call another function that does the job with the retured value.

Ricardo Pieper
  • 2,613
  • 3
  • 26
  • 40
-1

i think u should try data.responseText();

Jens
  • 20,533
  • 11
  • 60
  • 86
Rohith K
  • 1,433
  • 10
  • 15
  • You should provide more detail on how to use `data.responseText()` in the code example of @user61629. – Jens Jan 25 '15 at 15:31