dataType AJAX attribute - Jquery

Asked

Viewed 547 times

1

I know that with dataType set to XML, he understands that the return is XML and I can navigate the returned object as if I navigated by DOM, so I set it to HTML(dataType:"html"), and I hoped that his return was in fact a document, that I could browse, 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?

  • 1

    what you send from PHP to ajax?

  • an entire HTML page, the return exists, I saw in the reply and in fact it returns the page in the request.

1 answer

1


In the documentation of jQuery can be 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, some browsers filter out Certain Elements such as <html>, <title>, or <head> Elements. As a result, the Elements inserted may not be Representative of the original string passed.

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

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

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

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

Example: http://jsfiddle.net/11ocj5m5/

(and notice that the first past element is not even the body)

  • 1

    Perfect answer @Sergio, this I never imagined, thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.