How to use getJSON, sending data with post method?

后端 未结 7 998
暗喜
暗喜 2020-11-29 17:45

I am using above method & it works well with one parameter in URL.

e.g. Students/getstud/1 where controller/action/parameter format is applied.

相关标签:
7条回答
  • 2020-11-29 18:28

    Just add these lines to your <script> (somewhere after jQuery is loaded but before posting anything):

    $.postJSON = function(url, data, func)
    {
        $.post(url, data, func, 'json');
    }
    

    Replace (some/all) $.getJSON with $.postJSON and enjoy!

    You can use the same Javascript callback functions as with $.getJSON. No server-side change is needed. (Well, I always recommend using $_REQUEST in PHP. http://php.net/manual/en/reserved.variables.request.php, Among $_REQUEST, $_GET and $_POST which one is the fastest?)

    This is simpler than @lepe's solution.

    0 讨论(0)
  • 2020-11-29 18:29

    I just used post and an if:

    data = getDataObjectByForm(form);
    var jqxhr = $.post(url, data, function(){}, 'json')
        .done(function (response) {
            if (response instanceof Object)
                var json = response;
            else
                var json = $.parseJSON(response);
            // console.log(response);
            // console.log(json);
            jsonToDom(json);
            if (json.reload != undefined && json.reload)
                location.reload();
            $("body").delay(1000).css("cursor", "default");
        })
        .fail(function (jqxhr, textStatus, error) {
            var err = textStatus + ", " + error;
            console.log("Request Failed: " + err);
            alert("Fehler!");
        });
    
    0 讨论(0)
  • 2020-11-29 18:31

    $.getJSON() is pretty handy for sending an AJAX request and getting back JSON data as a response. Alas, the jQuery documentation lacks a sister function that should be named $.postJSON(). Why not just use $.getJSON() and be done with it? Well, perhaps you want to send a large amount of data or, in my case, IE7 just doesn’t want to work properly with a GET request.

    It is true, there is currently no $.postJSON() method, but you can accomplish the same thing by specifying a fourth parameter (type) in the $.post() function:

    My code looked like this:

    $.post('script.php', data, function(response) {
      // Do something with the request
    }, 'json');
    
    0 讨论(0)
  • 2020-11-29 18:32

    The $.getJSON() method does an HTTP GET and not POST. You need to use $.post()

    $.post(url, dataToBeSent, function(data, textStatus) {
      //data contains the JSON object
      //textStatus contains the status: success, error, etc
    }, "json");
    

    In that call, dataToBeSent could be anything you want, although if are sending the contents of a an html form, you can use the serialize method to create the data for the POST from your form.

    var dataToBeSent = $("form").serialize();
    
    0 讨论(0)
  • 2020-11-29 18:32

    I had code that was doing getJSON. I simply replaced it with post. To my surprise, it worked

       $.post("@Url.Action("Command")", { id: id, xml: xml })
          .done(function (response) {
               // stuff
            })
            .fail(function (jqxhr, textStatus, error) {
               // stuff
            });
    
    
    
        [HttpPost]
        public JsonResult Command(int id, string xml)
        {
              // stuff
        } 
    
    0 讨论(0)
  • 2020-11-29 18:36

    if you have just two parameters you can do this:

    $.getJSON('/url-you-are-posting-to',data,function(result){
    
        //do something useful with returned result//
        result.variable-in-result;
    });
    
    0 讨论(0)
提交回复
热议问题