Home > front end >  Can I send a post request to a js file instead of php?
Can I send a post request to a js file instead of php?

Time:12-11

First of all I don't want to interact with the server in this transaction. I'm using the fetch api to send a request but in url I want to pass a js file. Something like this-

    fetch('includes/handlers/handle.js', {
        method: 'POST',
        headers: h,
        body: body
    })

Also if it's possible can we be getting the POST variables using JS only?

I know this might seem like it doesn't make any sense to do it this way. But I'm trying something and would really appreciate any help.

CodePudding user response:

You can make a POST request to any URL you like.

You can't avoid interacting with the server when making requests to HTTP URLs. (And only HTTP URLs support POST requests).

It is up to the server to process that request.

You can write server-side code using JavaScript with tools like Node.js or Deno (Node.js being the most common approach).

Typically, when you do so, URLs where POST request bodies will be processed will not have .js in the URL - that is normally only used to serve static files to the client.


You cannot read the body of a POST request with client-side JS.

CodePudding user response:

It's quite odd, but I do think it's possible. You should remove any static serving for .js files on your server, and differentiate between static client side js files & http request handlers.

  • Related