Ajax Returning all page HTML

Asked

Viewed 625 times

0

I have an AJAX request on a customer’s website on the product page, which when selecting the available size of the product changes the price, but on the return of AJAX instead of only taking the price it takes all the HTML of the page, Which means he uploads the entire page at the altered price. What I want is to take only the price.

I believe it is conflict with the URL friendly, I do not know for sure, I have tried everything, even tried to pull the data via JSON but it does not work.

What would be the cause of this problem?

  • Can you post your code? Which tool do you use? Use the edit button http://answall.com/posts/136042/edit to add more details.

  • Itallo, post the font to help us help you :) Tip: In the Ajax request check the datatype you are using. http://www.w3schools.com/jquery/ajax_ajax.asp

  • You may have a header include where the ajax request is processed, post the source code you are working on for more details...

1 answer

1

You can take only the contents of a div specific on the return of Ajax.

For example, suppose the page you are on has a div with id #preco with the value of R$ 100,00:

<div id="preco">
    R$ 100,00
</div

Now you want to take the new value that returns from Ajax and it is returning all the HTML of the page, but you just want the content of div #preco to update to the page you are on. Say you return the HTML below in Ajax:

<html>
  <head>
  </head>
  <body>
     <h1>Bla Bla</h1>
     <div id="preco">R$ 200,00</div>
  </body>
</html>

You want to take the amount that’s on div #preco of HTML (R$ 200,00) returned and play on div #preco of the current page. For this, you can use the code below in pure Javascript:

var http = false;
http = new XMLHttpRequest();

var url_="pagina.php";
http.open("GET",url_,true);
http.onreadystatechange=function(){
   if(http.readyState==4){
      var html = http.responseText;
      var html = new DOMParser().parseFromString(html, "text/html");
      document.getElementById('preco_original').innerHTML = html.getElementById('preco').innerHTML.trim();
   }
}
http.send(null);

Or in jQuery:

$.ajax({
    url: 'pagina.php',
    dataType: 'html',
    success: function(html) {
        $("#preco").html($(html).filter('#preco').text().trim());
    }
});

Browser other questions tagged

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