Difference between "Get”, "POST".
HTTP stands for Hypertext Transfer Protocol and used for communication from client to server - and vice versa. While working on web development, in daily routine, you must play with HTTP methods it may be Get or Post.
Between web and server, a browser will be the client and our hosted application will be server.
For Example, when a browser sends an HTTP request to the application(server), in response our application/server returns the status of the request and may have some content also.
GET: mostly a GET is used to get or retrieve the data from the server. it should not have any side effects. In GET we must embed our data in the URI of request that is the only way. before performing any operation on the data from GET we should first verify. GET request can also be cached by browser and remain in browser history. So, avoid using GET Request when performing some sensitive data operations.
syntax for GET request:
$.get(URL,callback);
Simple call to GET data and alert:
$.get("tutorial_test.aspx", function(data, current_status){
alert("Here Data: " + data + "\nStatus of Request: " + current_status);
});
We can set other parameters like: cache parameter false when using ajax GET request like:
$.ajax({ type: "GET", url: "http://domain.com/returnData.html", cache: false, success: function(data){ $("#divResults").append(data); } });
POST: POST method is used to send data to the server. normally in WebApi or Web Services POST is used to insert some record or bunch of data to the database or any file. In POST request, we specify that which type of data we are going to send to the server and then server process the data accordingly. POST request response is not cached by the browser because our POST may have some side effect to the server. POST request should have HTTP Request Body having data to be submitted and in response we can also have response body.
syntax for POST request:
$.post(URL,data,callback);
First and important parameter is URL we want to send request. Secondly, we send our data object which contains data to be processed. In last we have a callback function through which we can check the response and status of the request. Now let have a little example with data, how we will pass data to POST request:
$.post("tutorial_test_post.aspx",
{
name: "Final Coding",
city: "Dubai"
},
function(data, Currstatus){
alert("Response Data: " + data + "\nStatus Received is : " + status);
});
No comments:
Post a Comment