简体   繁体   中英

Trying to understand JQuery/Ajax Get/POST calls

Correct me if I'm wrong but it was my understanding that a POST was to be used if I was changing data, and a GET was to be used if I want to retrieve data.

Based on that assumption.

I have (MVC5) app.

My JavaScript

function MyLoadData(myValue) {
    $.ajax({
        method: 'POST',
        url: '/Home/GetMyData',
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify({ "MyValue": myValue}),
        success: function (data) {
             // Do Stuff
        }                    
    });

and my controller.

public JsonResult GetMyData(string myValue)
        {  // Do Stuff }

This only works if I set the method: 'POST', if I set it to 'GET' it will still make the server call but not pass the myValue to the controller.

Also of note there is no data annotation on the GetMyData method.

In this scenario shouldn't I be using GET to get my data from the controller?

UPDATED based on comments:

function MyLoadData(myValue) {
    $.ajax({
        method: 'POST',
        url: '/Home/GetMyData',
        dataType: 'json',
        data: { "MyValue": myValue},
        success: function (data) {
             // Do Stuff
        }                    
    });

Both POST and GET methods can pass the myValue to the controller.

  • GET - Requests data from a specified resource
  • POST - Submits data to be processed to a specified resource

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data.

POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

The primary difference between a GET and a POST is that the POST will also submit the form data. In your example, you can use a GET by appending ?MyValue=<myValue> to your URL and WebAPI will assign the value to the Action's parameter.

If the GET request needs to work then use this code block:

function MyLoadData(myValue) {
$.ajax({
    method: 'GET',
    url: '/Home/GetMyData?myValue=test',
    success: function (data) {
         // Do Stuff
    }                    
});

Basically, you can use GET or POST to get the data. but in GET, the data is passed through query string. In POST it can be passed both through query string as well as body.

One real world scenario when to use POST-Suppose your method expects Customer parameter and you need to send Customer object as parameter, then you can send the json representation of Customer object through body.but its not possible through GET.

One more reason is security,if you use GET, your method can be called through browser.but if you use POST, the method can't be directly called.

These were the important difference.For more differences see this link - http://www.diffen.com/difference/GET_(HTTP)_vs_POST_(HTTP)

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