如何使用getJSON,用post方法发送数据?(How to use getJSON, sending data with post method?)

编程入门 行业动态 更新时间:2024-10-28 12:30:13
如何使用getJSON,用post方法发送数据?(How to use getJSON, sending data with post method?)

我使用上面的方法,它可以很好地与URL中的一个参数。

例如,应用控制器/动作/参数格式的Students/getstud/1 。

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

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

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

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

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.

Now I have an action in Students controller that accepts two parameters and return a JSON object.

So how do I post data with $.getJSON() using post method?

Similar methods are also acceptable.

The point is to call an action of the controller with AJAX.

最满意答案

$ .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();

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();

更多推荐

本文发布于:2023-07-22 12:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   方法   数据   getJSON   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!