Difference Between $.getJSON() and $.ajax() in jQuery

后端 未结 7 2300
星月不相逢
星月不相逢 2020-11-27 11:48

I am calling an ASP.NET MVC action

public JsonResult GetPatient(string patientID)
{
...

from JavaScript using jQuery. The following call wo

相关标签:
7条回答
  • 2020-11-27 12:18

    Replace

    data: { patientID: "1" },
    

    with

    data: "{ 'patientID': '1' }",
    

    Further reading: 3 mistakes to avoid when using jQuery with ASP.NET

    0 讨论(0)
  • 2020-11-27 12:22

    .getJson is simply a wrapper around .ajax but it provides a simpler method signature as some of the settings are defaulted e.g dataType to json, type to get etc

    N.B .load, .get and .post are also simple wrappers around the .ajax method.

    0 讨论(0)
  • 2020-11-27 12:22

    There is lots of confusion in some of the function of jquery like $.ajax, $.get, $.post, $.getScript, $.getJSON that what is the difference among them which is the best, which is the fast, which to use and when so below is the description of them to make them clear and to get rid of this type of confusions.

    $.getJSON() function is a shorthand Ajax function (internally use $.get() with data type script), which is equivalent to below expression, Uses some limited criteria like Request type is GET and data Type is json.

    Read More .. jquery-post-vs-get-vs-ajax

    0 讨论(0)
  • 2020-11-27 12:34

    Content-type

    You don't need to specify that content-type on calls to MVC controller actions. The special "application/json; charset=utf-8" content-type is only necessary when calling ASP.NET AJAX "ScriptServices" and page methods. jQuery's default contentType of "application/x-www-form-urlencoded" is appropriate for requesting an MVC controller action.

    More about that content-type here: JSON Hijacking and How ASP.NET AJAX 1.0 Avoids these Attacks

    Data

    The data is correct as you have it. By passing jQuery a JSON object, as you have, it will be serialized as patientID=1 in the POST data. This standard form is how MVC expects the parameters.

    You only have to enclose the parameters in quotes like "{ 'patientID' : 1 }" when you're using ASP.NET AJAX services. They expect a single string representing a JSON object to be parsed out, rather than the individual variables in the POST data.

    JSON

    It's not a problem in this specific case, but it's a good idea to get in the habit of quoting any string keys or values in your JSON object. If you inadvertently use a JavaScript reserved keyword as a key or value in the object, without quoting it, you'll run into a confusing-to-debug problem.

    Conversely, you don't have to quote numeric or boolean values. It's always safe to use them directly in the object.

    So, assuming you do want to POST instead of GET, your $.ajax() call might look like this:

    $.ajax({
      type: 'POST',
      url: '/Services/GetPatient',
      data: { 'patientID' : 1 },
      dataType: 'json',
      success: function(jsonData) {
        alert(jsonData);
      },
      error: function() {
        alert('Error loading PatientID=' + id);
      }
    });
    
    0 讨论(0)
  • 2020-11-27 12:34

    The only difference I see is that getJSON performs a GET request instead of a POST.

    0 讨论(0)
  • 2020-11-27 12:34
    contentType: 'application/json; charset=utf-8'
    

    Is not good. At least it doesnt work for me. The other syntax is ok. The parameter you supply is in the right format.

    0 讨论(0)
提交回复
热议问题