Can you use post requests?

Hey, I just have a quick question, is there a way to write to a file using a post or create request? If not is there a way to make a listener for post requests using javascript?

So what are you actually asking? You question starts with 'is there a way to write to a file', and then end it talking about POST requests and JS.

Please clarify what your asking.

3 Likes

I am asking if you can create, delete, or edit a file from the website using JavaScript, I don’t see how that isn’t clear.

yeah i dont think it is clear
Answer= If u know how to code it
If u wanna use php = PHP File Create/Write

Thankfully, it’s not possible to create a file straight on a website straight from the browser. It would be really bad if you could create arbitrary files on any website server by pasting some Javascript in the developer console.

What you can do is write PHP code which can create the file, and talk to that PHP script from your Javascript code with AJAX.

3 Likes

Ok, building off of @admin’s reply, JavaScript is a client-side language.

When a visitor enters in your website, it gets sent to a DNS resolver which looks up the IP address associated with the website and sends it back to the visitor’s device.

The visitor’s device then attempts to connect to the IP address.

Your server detects the incoming requests and fires a new program instance to compensate for the request (AKA your php file running and sending the result).

If the file is a JavaScript file, the it gets sent to the client where the user’s browser handles the file (usually via execution).

Because of this process, JavaScript is not “attached” to your server. It gets executed on the client and PHP gets executed by the server.

It’s like asking someone to shake hands with someone else halfway across the world… not gonna work!

However, WhatWG and W3 knew this frustration, and so they built a way to communicate to the server using JavaScript - AJAX!

AJAX (some people think it stands for “Asynchronous JavaScript And XML” but it doesn’t) is a way to communicate between the client and the server.

AJAX is now replaced by websockets for rapid back-and-forth data, but it is still used a lot today.

Beware though, as anyone can imitate an AJAX call.

One rule I like to go by is never trust anything sent to you by a client.

You could create a file by AJAXing data to the server where it creates a file based off of that data (whatever you want to do with it).

I hope this increases your intelligence on the subject!

3 Likes

I have an idea for a security system :smiley:

What is it?

Most likely it has to be verifying something on the server that only the person with legitimate access would know (like a login system).

It is essentially like a request key, you have to be given one by the server before you can send data, and it only works once

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.