0

I was reading something about Ajax and jQuery and I saw this code:

$.ajax({
  url: "test.html",
  cache: false
}).done(function( html ) {
  $("#results").append(html);
});

I don't see anywhere the declaration of "html", how will the code know what to append?

P.S. It might be a stupid question, but i didn't find the answer to this question anywhere :/

1 Answer 1

1

When the ajax call completes it will pass data into the function defined for done.

.done(function(html){

html is not "defined" here, but rather the name of the variable that receives the data. Once the placeholder is defined you can use it anywhere in your function.

For example, if you have

function myFunc(foo){
    alert(foo);
}

and then did

myFunc(1234);
//result 1234

myFunc("test");
//result "test"

So, really html is just what this person chose to call the data that is received from the ajax call. If you read the tutorial's on jQuery's site, most of the time they name this variable "data"

Sign up to request clarification or add additional context in comments.

2 Comments

So in the php file i have to call a var "html"? In example, if i have $data = "Hello World!" the ajax code will return "Hello World", right?
PHP cannot talk to javascript directly because it is processed in client side in the browsers whereas PHP is processed server side. If you have a php page called test.php with the code <% echo 'Hello World" %> the javascript variable "html" would contain "Hello World" with a successful response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.