AJAX request returns entire HTML page instead of just the data I wanted it to echo

vytasK

I have:

Index.php

      <?php
   if(isset($_POST["id"])){
     echo($_POST["id"]);
   }    
       ?>

JavaScript:

 let xhttp = new XMLHttpRequest();
        xhttp.open("POST", "index.php", true);
        xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
        xhttp.onreadystatechange = function (){
            if(xhttp.readyState == 4 && xhttp.status == 200)
            {
                let result = xhttp.responseText;
                console.log(result)
            }
        }
        xhttp.send("id=195");

But the console prints the whole HTML code for the php page. I thought it should print just the ID.

ADyson

It sounds like you have other code in index.php, then?

If you only want it to print the ID, add an exit(); command just after it echoes the ID, so that the rest of the script is not processed in that situation. (That's assuming that the PHP code comes before the HTML in the file, of course.)


P.s. Most developers tend to separate the PHP which responds to Ajax requests from the PHP which deals with loading HTML pages. This

a) prevents the type of problem you're experiencing,

b) keeps the functionality clean and clear and separate, making unit testing a lot easier, and

c) takes a step to it being more like a fully-fledged API which just deals with data - and can potentially be called from other applications, services or front-ends too, if that's useful.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wordpress ajax call returns entire html page

From Dev

Why is Ajax.ActionLink updating entire page instead of just UpdateTarget

From Dev

Laravel Ajax request returning whole page rather than just data

From Dev

Laravel Ajax request returning whole page rather than just data

From Dev

BS4 returns [] instead of the wanted HTML tag

From Dev

i need to return an entire html page using an ajax call

From Dev

R scraping entire html, not just page view

From Java

GET request returns index.html doc instead of json data

From Dev

HTTP post request returns HTML code instead of JSON data

From Dev

HTTP post request returns HTML code instead of JSON data

From Dev

Request ajax writes the data and returns ok but brings json and html

From Dev

Jquery Ajax call doesn't hit the action method/returns entire HTML page

From Dev

Jquery Ajax call doesn't hit the action method/returns entire HTML page

From Dev

reload just a part of a page after ajax request

From Dev

WordPress AJAX - Returns entire HTML markup in response

From Dev

pf blocks all in/out traffic instead of just the one port I wanted to block

From Dev

Can I clone just the latest changesets of a repository instead of the entire history?

From Dev

jQuery AJAX GET to a Servlet doesn't show the JSP page - instead returns with HTML code

From Dev

jQUery ajax call to asp.net webforms returns html page instead of calling the specified method in the url

From Dev

I need jqgrid to refresh the entire page, not just the grid

From Dev

How can I replace the entire HTML of a page, or its body content, via an AJAX call's success callback?

From Dev

Django request data returns str instead of list

From Dev

ajax returns HTML source code instead of String

From Dev

Webapi returns html page instead of json

From Dev

OpenWhisk returns whole response object instead of just the data

From Dev

cart.php returns wanted values just for the last product

From Dev

page refreshing instead of displaying ajax data

From Dev

page refreshing instead of displaying ajax data

From Dev

CodeIgniter: AJAX returns html content of current page

Related Related

  1. 1

    Wordpress ajax call returns entire html page

  2. 2

    Why is Ajax.ActionLink updating entire page instead of just UpdateTarget

  3. 3

    Laravel Ajax request returning whole page rather than just data

  4. 4

    Laravel Ajax request returning whole page rather than just data

  5. 5

    BS4 returns [] instead of the wanted HTML tag

  6. 6

    i need to return an entire html page using an ajax call

  7. 7

    R scraping entire html, not just page view

  8. 8

    GET request returns index.html doc instead of json data

  9. 9

    HTTP post request returns HTML code instead of JSON data

  10. 10

    HTTP post request returns HTML code instead of JSON data

  11. 11

    Request ajax writes the data and returns ok but brings json and html

  12. 12

    Jquery Ajax call doesn't hit the action method/returns entire HTML page

  13. 13

    Jquery Ajax call doesn't hit the action method/returns entire HTML page

  14. 14

    reload just a part of a page after ajax request

  15. 15

    WordPress AJAX - Returns entire HTML markup in response

  16. 16

    pf blocks all in/out traffic instead of just the one port I wanted to block

  17. 17

    Can I clone just the latest changesets of a repository instead of the entire history?

  18. 18

    jQuery AJAX GET to a Servlet doesn't show the JSP page - instead returns with HTML code

  19. 19

    jQUery ajax call to asp.net webforms returns html page instead of calling the specified method in the url

  20. 20

    I need jqgrid to refresh the entire page, not just the grid

  21. 21

    How can I replace the entire HTML of a page, or its body content, via an AJAX call's success callback?

  22. 22

    Django request data returns str instead of list

  23. 23

    ajax returns HTML source code instead of String

  24. 24

    Webapi returns html page instead of json

  25. 25

    OpenWhisk returns whole response object instead of just the data

  26. 26

    cart.php returns wanted values just for the last product

  27. 27

    page refreshing instead of displaying ajax data

  28. 28

    page refreshing instead of displaying ajax data

  29. 29

    CodeIgniter: AJAX returns html content of current page

HotTag

Archive