Laman

Manual Javascript load another page

<div id="load"></div>
<script>
// ========= get method
var client = new XMLHttpRequest();
client.open('GET', 'http://<yourweb>/contentpage.php');
client.onreadystatechange = function() {
    document.getElementById('load').innerHTML=(client.responseText);
}
client.send();

// ========= post method
var http = new XMLHttpRequest();
var url = 'get_data.php';
var params = 'lorem=ipsum&para=meter';
http.open('POST', url, true);

//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        console.log(http.responseText);
    }
}
http.send(params);
</script>

In another host page set <?php header("Access-Control-Allow-Origin: *");?>

No comments:

Post a Comment

Silahkan