Home > Backend Development > PHP Problem > How php responds to ajax request

How php responds to ajax request

angryTom
Release: 2023-02-27 12:52:02
Original
4109 people have browsed it

How php responds to ajax request

How does php respond to ajax requests?

Due to a lot of front-end IT, when rendering the page, it needs to be obtained from the background JSON data (of course other types of data can also be obtained) needs to be responded to the front-end page from the background. This is a very simple case of transmitting it to the front-end in PHP array format:

1. JavaScript request:

var container = document.getElementsByClassName('container')[0];
container.onclick = function()
{
    var xhr = new XMLHttpRequest();
    xhr.open('GET','/learn/php_ajax/responseExample.php',true);
    xhr.send();
 
    xhr.onload = function(ev)
    {
        var data = JSON.parse(ev.currentTarget.responseText);
        console.log(data);
    };
}
Copy after login

2. PHP simple background response

The parameters of the ajax request can be obtained through $_GET and $_POST, but there is no corresponding display here.

$array = array("a" => "视频", "b"=>"教2程", "c" => "六");
exit(json_encode($array));
Copy after login

3. Get the results:

{
    a: "视频",
    b: "教2程",
    c: "六"
}
Copy after login

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How php responds to ajax request. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template