代码之家  ›  专栏  ›  技术社区  ›  Vikas

如何使用getjson,使用post方法发送数据?

  •  101
  • Vikas  · 技术社区  · 16 年前

    我正在使用上面的方法,它与URL中的一个参数一起工作得很好。

    例如 Students/getstud/1 其中,控制器/动作/参数格式适用。

    现在我在学生控制器中有一个操作,它接受两个参数并返回一个JSON对象。

    那么我如何发布数据 $.getJSON() 使用post方法?

    也可采用类似的方法。

    重点是用Ajax调用控制器的操作。

    7 回复  |  直到 8 年前
        1
  •  208
  •   Erv Walter    16 年前

    $.getjson()方法执行http get而不是post。你需要使用 $.post()

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

    在那个电话里, dataToBeSent 可以是您想要的任何内容,但是如果要发送HTML表单的内容,则可以使用 serialize 方法从窗体中为日志创建数据。

    var dataToBeSent = $("form").serialize();
    
        2
  •  13
  •   Sahil Mahajan Mj ila123    11 年前

    这是我的“一线”解决方案:

    $.postJSON = function(url, data, func) { $.post(url+(url.indexOf("?") == -1 ? "?" : "&")+"callback=?", data, func, "json"); }
    

    为了使用jsonp和post方法,此函数将“callback”get参数添加到URL中。这是使用它的方法:

    $.postJSON("http://example.com/json.php",{ id : 287 }, function (data) {
       console.log(data.name);
    });
    

    服务器必须准备好处理回调get参数并将json字符串返回为:

    jsonp000000 ({"name":"John", "age": 25});
    

    其中“jsonp000000”是回调get值。

    在PHP中,实现如下:

    print_r($_GET['callback']."(".json_encode($myarr).");");
    

    我做了一些跨领域的测试,它似乎可以工作。但是仍然需要更多的测试。

        3
  •  5
  •   Community CDub    8 年前

    只需将这些行添加到 <script> (在加载jquery之后,但在发布任何内容之前):

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

    替换(部分/全部) $.getJSON 具有 $.postJSON 享受吧!

    您可以使用与 盖特森 . 不需要服务器端更改。(嗯,我总是建议使用 $_REQUEST 在PHP中。 http://php.net/manual/en/reserved.variables.request.php , Among $_REQUEST, $_GET and $_POST which one is the fastest? )

    这比@lepe的解决方案简单。

        4
  •  3
  •   Stan Bashtavenko    11 年前

    我有执行getjson的代码。我只是把它换成了波斯特。令我惊讶的是,它起了作用

       $.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
        } 
    
        5
  •  2
  •   Fusca Software    8 年前

    我刚刚用了post和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!");
        });
    
        6
  •  1
  •   Vince Bowdren    8 年前

    $.getJSON() 对于发送Ajax请求和作为响应返回JSON数据非常方便。唉,jquery文档缺少一个应该命名的姊妹函数 $.postJSON() . 为什么不只用 $GETJSON() 完成了吗?嗯,也许你想发送大量的数据,或者,在我的例子中,IE7只是不想在GET请求中正常工作。

    是的,目前没有 $PASJSON() 方法,但可以通过在 $.post() 功能:

    我的代码如下:

    $.post('script.php', data, function(response) {
      // Do something with the request
    }, 'json');
    
        7
  •  -8
  •   robert vano    13 年前

    如果只有两个参数,则可以执行此操作:

    $.getJSON('/url-you-are-posting-to',data,function(result){
    
        //do something useful with returned result//
        result.variable-in-result;
    });