简体   繁体   中英

Explaining an HTTP “GET” request

I have many times successfully implemented HTTP "POST" requests in my projects, more importantly because i could understand how the request worked in my code.

Now i need to make a "GET" request , but i am a bit confused on how i can accomplish it.

I have an html-jQuery page , that when launched sends an "id" (POST request) to my PHP server. Then , i need my PHP server , to look in the database in which events this "id" is subscribed to and return the events.

So i need to make a "GET" request , where i send as parameter the "id" and i get back a json file with the events. However how this PHP file would look like? I mean , i can write a php script that accepts an "id" as input ,makes the right SQL queries to look for the events and create a json file with them. But how do i return it to my html-jquery page though?

Or should the "GET" request target only XML/JSON files? The way i see what i described till now is that my jquery makes a POST request to my server with the id and the server makes a POST request to my web page with the events. Is not exactly a "GET" ...

There are multiple ways to do what you want.

With JQuery and Ajax it would look like this:

$.get("yourpage.php", {id: 5}, function(jsonResult){

 // do whatever with the json result from the server

});

in "yourpage.php"

$id = $_GET["id"];
//do database query etc


$result = array("yourdata" => "yourvalue")

echo json_encode($result);

Have a look at http://api.jquery.com/jQuery.get/ or http://api.jquery.com/jQuery.getJSON/

And you should learn about the basics of http requests / responses. It seems like you don't really know what you are doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM