如何使用的getJSON,用post方法发送数据? [英] How to use getJSON, sending data with post method?
问题描述
我使用上面的方法和放大器;它与URL一个参数效果很好。
I am using above method & it works well with one parameter in URL.
例如。 学生/ 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.
那么,如何与发布 $数据的getJSON()
使用POST方法?
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没有。您需要使用 $。员额()
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屋!