2009-04-15 57 views
93

我正在使用上面的方法&它与URL中的一个参数一起工作良好。如何使用getJSON,使用post方法发送数据?

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

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

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

类似的方法也是可以接受的。

问题的关键是用AJAX调用控制器的动作。

+4

`GET`在`getJSON`意味着*使用GET *得到一些JSON。 – 2012-08-17 21:36:54

+0

@Majid Fouladpour当我问这个问题时,我并不知道..! – Vikas 2012-08-18 05:54:08

回答

190

$ .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方法从POST创建数据的形成。

var dataToBeSent = $("form").serialize(); 
+5

只想添加$ .getJSON支持Jsonp(跨域访问),不幸的是$ .post不支持。 – Tomas 2010-09-28 12:24:38

+1

其实.getJSON()支持两种跨​​域访问。 JSONP,它不使用GET或POST,但脚本注入;还有CORS - 和.post()也支持CORS。但CORS要求服务器也支持它,而JSONP则不支持。 – hippietrail 2011-12-22 08:55:08

+2

不正确,JSONP也需要服务器支持来解析回调参数。 – Shrulik 2013-05-31 18:47:16

-8

,如果你只有两个参数,你可以这样做:

$.getJSON('/url-you-are-posting-to',data,function(result){ 

    //do something useful with returned result// 
    result.variable-in-result; 
}); 
12

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

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

为了使用JSONP和POST方法,该函数将“回调”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

我有做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 
    } 
1

$.getJSON()是Ajax请求并取回JSON数据作为响应非常方便。唉,jQuery文档缺少应该命名为$.postJSON()的姐妹功能。为什么不使用$.getJSON()并完成它?那么,也许你想发送大量的数据,或者在我的情况下,IE7只是不希望与GET请求正常工作。

这是真的,目前还没有$.postJSON()方法,但你可以通过在$.post()功能指定第四个参数(类型)完成同样的事情:

我的代码是这样的:

$.post('script.php', data, function(response) { 
    // Do something with the request 
}, 'json'); 
3

这些线路只需添加到您的<script>(jQuery是加载之后的地方,但任何发布前):

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

$.postJSON替换(某些/全部)$.getJSON并享受!

您可以使用与$.getJSON相同的Javascript回调函数。 不需要服务器端更改。 (嗯,我总是推荐在PHP中使用$_REQUESThttp://php.net/manual/en/reserved.variables.request.php,Among $_REQUEST, $_GET and $_POST which one is the fastest?

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

1

我只是用后和如果:

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!"); 
    }); 
相关问题