Send json object using POST pure javascript [duplicate] - javascript

This question already has answers here:
Reading JSON POST using PHP
(3 answers)
Closed 7 years ago.
I want to send a JSON object to PHP server but I get an empty array there. Why is this happening?
var obj = {
username: username,
password: password
};
var ajax = new XMLHttpRequest();
ajax.open('post', 'ajax/login_register.php');
ajax.setRequestHeader("Content-type", "application/json;charset=UTF-8");
ajax.send(JSON.stringify(obj));

You need to give it a name that you can reference on the server side.
ajax.send('user=' + encodeURIComponent(JSON.stringify(obj)));
$_POST['user'] // <-- your JSON string

Related

Waning: Undefined array key when trying to store post variables (xmlhttprequest) [duplicate]

This question already has answers here:
Receive JSON POST with PHP
(12 answers)
Closed 12 months ago.
I'm trying to save the data that "buchen.php" get in php variables but I keep getting the following error
Warning: Undefined array key "reise_id" in buchen.php on line 4
Warning: Undefined array key "platz_geb" in buchen.php on line 5
Can anyone tell what could be wrong here?
var xhr = new XMLHttpRequest();
xhr.open('POST', 'buchen.php');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
reise_id: "<?php echo $reise_id; ?>",
platz_geb: platz_blau
}));
//buchen.php
$reise_id = $_POST['reise_id'];
$platz_geb = $_POST['platz_geb'];
Instead of grabbing the POST variables as always, try the following on the PHP code:
$json = file_get_contents('php://input');
echo $json;

How to receive AJAX data in php [duplicate]

This question already has answers here:
Receive JSON POST with PHP
(12 answers)
Closed 2 years ago.
I am trying to build a small form in my website which will use ajax to save the form details to the database with php. I can do it with jquery, since I am a student, I don't want to use any external libraries.
I was able to use ajax "get method" and even manage to create a post method but I have no idea how to receive this data in the php script and process it.
This is the ajax code i used to send json data
subForm.addEventListener('click',()=>{
var forms = {
"name": document.getElementById('name').value,
"phone": document.getElementById('phone').value,
"email": document.getElementById('email').value,
"message": document.getElementById('message').value,
"exe" : true
}
var jString = JSON.stringify(forms);
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "recieve.php");
xhttp.setRequestHeader("Content-Type" , "application/json")
xhttp.send(jString);
});
If you've managed to process a GET. You'll manage the POST just in the same way. By peeking in the global variable $_POST['myJson'] in your "recieve.php".
On the client side, you'd need then to stringify like so:
var jString = JSON.stringify({"myJson": forms});
Therefore, on the server side, $_POST['myJson'] will contain the object 'forms', which you constructed on the client side. And if you want to access let's say, the name property value, you do like so:
var nameValue = $_POST['myJson']['name'];

How can I send data to Flask Server? [duplicate]

This question already has answers here:
How do I post form data with fetch api?
(11 answers)
Fetch: POST JSON data
(17 answers)
Get the data received in a Flask request
(23 answers)
Closed 2 years ago.
I'm new using flask or JS, as that, I can't find a way to do what I want to.
I created a webserver using flask (in python) and it uses index.html as main page, I wanted to update data to the server every few secounds (maybe 1-3 secs). The thing is, I don't have any form to work with, or even queries and I don't know what else can I work with. The data I want to send is small string to be saved on server host later on.
<body>
<center>
<button onmousedown="sendDirectionKey('^')">^</button>
...
</center>
</body>
<script>
function sendDirectionKey(Key)
{
...
sendData(data_string);
}
function sendData(data)
{
...
}
</script>
An easy modern solution is the fetch() API:
function sendData(data)
{
const body = new FormData();
body.append("key", data);
return fetch("/receive", {method: "POST", body, credentials: "include"});
}
The equivalent receiver on the Python side would be something like
#app.route("/receive", methods=["POST"])
def receive():
print(request.form) # should have a `key` key

ArrayBuffer when stringified becomes empty object [duplicate]

This question already has answers here:
How to save ArrayBuffer?
(3 answers)
How to post a file from a form with Axios
(9 answers)
Closed 4 years ago.
In my app, i'm uploading a file using FileReader and parsing it as an ArrayBuffer. The file properties are saved in an object, with structure like this:
file: {
name: 'fileName', // type string
content: ArrayBuffer // read like FileReader.readAsArrayBuffer(uploadedFile)
}
When I want to save the file to backend, I'm using axios, and sending a request like this:
axios({
url: "/api/v3/synchronous/commands",
method: "POST",
data: JSON.stringify(file),
headers,
})
The problem is that when it get's stringifed, content inside file becomes an empty object {}. How to go about this issue, without having to convert ArrayBuffer to something else, and then converting it back to ArrayBuffer?

How to securely send POST data between JQuery and PHP? [duplicate]

This question already has answers here:
How to send secure AJAX requests with PHP and jQuery
(3 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
I would like to send data from a Jquery script to a php page using POST.
My code is as follows:
$.ajax({
type: "POST",
url: 'http://mywebsite.com/add_data.php',
data: {value1: some_value,
value2: 123,
value3: ABC
},
My php script captures the data and records them in MySQL database.
$myvalue1 = $_POST['value1'];
$myvalue2 = $_POST['value2'];
$myvalue3 = $_POST['value3'];
The problem is that since JS code is visible in the source code, anyone can submit anything to my database...
Is there an easy way to make it more secure and prevent this from happening?
Any advice appreciated.
All data from the client always MUST be validated and sanitized, because all data from the client can be modificated/falsificated.

Categories

Resources