Created
March 31, 2017 19:32
-
-
Save webyroki/e2b7884511e18f494000b6f387fa53d2 to your computer and use it in GitHub Desktop.
Отправить данные из JS методом POST и сохранить в json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = 'index.json'; //куда сохраняем | |
if (file_get_contents("php://input")){ | |
$postdata = file_get_contents("php://input");// читаем данные в строку в raw формате | |
file_put_contents($file, $postdata, LOCK_EX); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<h1>The XMLHttpRequest Object</h1> | |
<button type="button" onclick="loadDoc()">Отправить</button> | |
<p id="demo"></p> | |
<script> | |
var event = { | |
title: "Конференция", | |
date: "сегодня" | |
}; | |
var str = JSON.stringify(event); | |
function loadDoc() { | |
var xhttp = new XMLHttpRequest(); | |
xhttp.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
document.getElementById("demo").innerHTML = this.responseText; | |
} | |
}; | |
xhttp.open("POST", "forms.html.php", true); | |
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
xhttp.send(str); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment