attribute dataType AJAX - JQuery

1

I know that with the dataType set to XML, it understands that the return is XML and I can navigate the returned object as if navigating through the DOM, so I set it to HTML ( dataType:"html" ), and I expected return of it was in fact a document, that I could navigate, but returns empty HTML tags, see my code:

$.ajax({
url:"test_login.php",
method:"POST",
dataType:"html",
data: {acao:"login",pass:pass,email:email},

success:function(data){
alert(
    $(data).find("html").text()
    );  // retorna vazio.


}

So, what am I missing?

    
asked by anonymous 11.03.2015 / 19:46

1 answer

1

The documentation for jQuery can read:

  

When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, jQuery uses the browser's .innerHTML property to parse the passed HTML and insert it into the current document. During this process, add browsers filter out certain elements such as <html> , <title> , or <head> elements. As a result, the inserted elements may not be representative of the original string passed.

Basically this means that whole pages can not be imported via AJAX, nor via jQuery(stringDePáginaInteira) , and that html elements of that string such as html , head and title are removed.

Then $(data).find("html") will be empty.

The correct way, and the reason that ajax is useful, is to ask via ajax only the minimum data needed. Otherwise it's better to reload the page or an iFrame with that content. And if you need parameters via GET.

That said, it is possible to import HTML via ajax, but in this case it is better to use more secure html tags, basically anything that can be inside body .

Example: link

(and note that the first element passed is not even body )

    
11.03.2015 / 20:41