ArrayBuffer when stringified becomes empty object [duplicate] - javascript

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?

Related

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

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.

Passing Javascript Variable to Python Flask [duplicate]

This question already has answers here:
jQuery posting JSON
(3 answers)
Submit a form using jQuery [closed]
(22 answers)
Closed 5 years ago.
I have read several postings on different examples for passing a javascript variable to flask through post/get forms. I still don't understand how to do this. From my understanding, the form creates a post/get that can then be called and received by the python flask script. Can someone write up a very simple example on what this should look like?
Starting from creating a variable with any value in javascript and then making the post/get. Lastly what should the receiving end on python should look like and finally print the variable from python.
How I did this was using an ajax request from the javascript which would look something like this. I think the easiest way would be using JQuery as well since it might be a bit more verbose with pure javascript.
// some movie data
var movies = {
'title': movie_title,
'release_date': movie_release_date
}
$.ajax({
url: Flask.url_for('my_function'),
type: 'POST',
data: JSON.stringify(movies), // converts js value to JSON string
})
.done(function(result){ // on success get the return object from server
console.log(result) // do whatever with it. In this case see it in console
})
Flask.url requires JSGlue which basically let's you use Flask's
url_for but with javascript. Look it up, easy install and usage. Otherwise I think you could just replace it with the url e.g '/function_url'
Then on the server side you might have something like this:
from flask import request, jsonify, render_template
import sys
#app.route("/function_route", methods=["GET", "POST"])
def my_function():
if request.method == "POST":
data = {} // empty dict to store data
data['title'] = request.json['title']
data['release_date'] = request.json['movie_release_date']
// do whatever you want with the data here e.g look up in database or something
// if you want to print to console
print(data, file=sys.stderr)
// then return something back to frontend on success
// this returns back received data and you should see it in browser console
// because of the console.log() in the script.
return jsonify(data)
else:
return render_template('the_page_i_was_on.html')
I think the main points are to look up ajax requests in jquery, flask's request.json() and jsonify() functions.
Edit: Corrected syntax

Send json object using POST pure javascript [duplicate]

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

Sanitizing POST data in AJAX request [duplicate]

This question already has answers here:
Send POST data using XMLHttpRequest
(13 answers)
Closed 3 years ago.
I'm sending AJAX request with POST params (without JQuery). Is there any function to sanitize characters like &?
You really don't need to do that in a modern browser:
var xhr = new XMLHttpRequest;
xhr.open("POST","yourURL");
xhr.onload = function(data){ /*onload hook */};
xhr.send({keyval:"data here"}); // data here!
As you can see, you can pass an object to the .send method and it'll send that, you don't need to encode or mess with URI components in the POST body (or GET url) at all.
you can of course also send form data
I think what you are looking for is something like this:
encodeURIComponent
Do you mean sanitize or encode? If encode is what you mean, use:
encodeURIComponent(value);
in your script, and to decode this in PHP use:
urldecode($_POST['key']);
to decode the value. If you meant sanitize, please elaborate.

Categories

Resources