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

查看:28
本文介绍了如何使用 getJSON,使用 post 方法发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用上述方法 &它适用于 URL 中的一个参数.

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

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

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

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

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

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

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

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

Similar methods are also acceptable.

重点是用 AJAX 调用控制器的一个动作.

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

推荐答案

$.getJSON() 方法执行 HTTP GET 而不是 POST.您需要使用 $.post()

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

在该调用中,dataToBeSent 可以是您想要的任何内容,但如果要发送 html 表单的内容,您可以使用 serialize 方法来为表单中的 POST 创建数据.

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

这篇关于如何使用 getJSON,使用 post 方法发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆