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

How php responds to ajax request

angryTom
angryTomOriginal
2019-10-19 14:32:214107browse

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);
    };
}

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));

3. Get the results:

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

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!

Statement:
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