4 Replies - 198 Views - Last Post: 13 March 2018 - 07:33 AM

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Confusion about ajax .post() request

Posted 13 March 2018 - 06:10 AM

Assume I have 2 files: One is an HTML file with an AJAX request and the second is the PHP file that receives that request.
The purpose of these 2 files is: To send request to the PHP file(from the HTML file), in order to get data from a database, and return this data back to the HTML file.

Now so far when I used POST requests (in registration/login forms), the POST request had parameters in them (username, password, etc)
But in this situation, all the ajax request needs to do is to "trigger" the PHP file to request data from the database, so do I leave the .post() parameter empty and only specify the url to the PHP file? Or I am missing something? (Is it even possible to make a POST request without parameters?)

Or maybe I need to completely use a different approach to do that?

thanks!

This post has been edited by Foobarer: 13 March 2018 - 06:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Confusion about ajax .post() request

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Confusion about ajax .post() request

Posted 13 March 2018 - 06:31 AM

If you want to have data, make an AJAX GET request (with a parameter that identifies the dataset you want).
Was This Post Helpful? 1
  • +
  • -

#3 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: Confusion about ajax .post() request

Posted 13 March 2018 - 06:42 AM

What do you mean by parameter? If I only need to get information?
Because my following code with POST actually worked.
How would you change it to your version? (how to change it to "GET" and where to add the "parameter" in my example, so that it still works?)

            $(document).ready(function () {
                $.ajax({
                    type: 'post',
                    url: 'phptest.php',
                    dataType: 'json',
                    success: function (response) {
                        
                        document.getElementById("change_me").innerHTML = response;
                    }
                });
            });

This post has been edited by Foobarer: 13 March 2018 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Confusion about ajax .post() request

Posted 13 March 2018 - 07:02 AM

Quote

What do you mean by parameter?

usually (and in the OP you didn't mention what kind of data you meant), when you fetch data from a database and you want only a single dataset, you need to tell the database which one.

However, it may be that the information can be fetched from somewhere else, e.g. the user id of the logged in user.
Was This Post Helpful? 1
  • +
  • -

#5 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: Confusion about ajax .post() request

Posted 13 March 2018 - 07:33 AM

It's actually the same example from my other post on the PHP section where you helped me (with the JSON data type)
And in my case I needed a single column, and it's already specified in the query (because I know beforehand which column I want)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1