jQuery 如何使用 getJSON,使用 post 方法发送数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/751218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use getJSON, sending data with post method?
提问by Vikas
I am using above method & it works well with one parameter in URL.
我正在使用上述方法,它适用于 URL 中的一个参数。
e.g. Students/getstud/1
where controller/action/parameter format is applied.
例如应用Students/getstud/1
控制器/动作/参数格式的地方。
Now I have an action in Students controller that accepts two parameters and return a JSON object.
现在我在 Students 控制器中有一个动作,它接受两个参数并返回一个 JSON 对象。
So how do I post data with $.getJSON()
using post method?
那么如何$.getJSON()
使用 post 方法发布数据呢?
Similar methods are also acceptable.
类似的方法也是可以接受的。
The point is to call an action of the controller with AJAX.
关键是用 AJAX 调用控制器的动作。
回答by Erv Walter
The $.getJSON() method does an HTTP GET and not POST. You need to use $.post()
$.getJSON() 方法执行 HTTP GET 而不是 POST。你需要使用$.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 serializemethod to create the data for the POST from your form.
在该调用中,dataToBeSent
可以是您想要的任何内容,但如果要发送 html 表单的内容,您可以使用serialize方法为表单中的 POST 创建数据。
var dataToBeSent = $("form").serialize();
回答by lepe
This is my "one-line" solution:
这是我的“单行”解决方案:
$.postJSON = function(url, data, func) { $.post(url+(url.indexOf("?") == -1 ? "?" : "&")+"callback=?", data, func, "json"); }
In order to use jsonp, and POST method, this function adds the "callback" GET parameter to the URL. This is the way to use it:
为了使用 jsonp 和 POST 方法,该函数在 URL 中添加了“回调”GET 参数。这是使用它的方法:
$.postJSON("http://example.com/json.php",{ id : 287 }, function (data) {
console.log(data.name);
});
The server must be prepared to handle the callback GET parameter and return the json string as:
服务器必须准备好处理回调 GET 参数并将 json 字符串返回为:
jsonp000000 ({"name":"John", "age": 25});
in which "jsonp000000" is the callback GET value.
其中“jsonp000000”是回调GET值。
In PHP the implementation would be like:
在 PHP 中,实现将类似于:
print_r($_GET['callback']."(".json_encode($myarr).");");
I made some cross-domain tests and it seems to work. Still need more testing though.
我做了一些跨域测试,它似乎有效。不过还需要更多的测试。
回答by Lerin Sonberg
Just add these lines to your <script>
(somewhere after jQuery is loaded but before posting anything):
只需将这些行添加到您的<script>
(加载 jQuery 之后但发布任何内容之前):
$.postJSON = function(url, data, func)
{
$.post(url, data, func, 'json');
}
Replace (some/all) $.getJSON
with $.postJSON
and enjoy!
更换(部分/全部)$.getJSON
与$.postJSON
和享受!
You can use the same Javascript callback functions as with $.getJSON
.
No server-side change is needed. (Well, I always recommend using $_REQUEST
in PHP. http://php.net/manual/en/reserved.variables.request.php, Among $_REQUEST, $_GET and $_POST which one is the fastest?)
您可以使用与 .js 相同的 Javascript 回调函数$.getJSON
。不需要服务器端更改。(好吧,我总是建议使用$_REQUEST
的PHP。http://php.net/manual/en/reserved.variables.request.php,其中$ _REQUEST,$ _GET和$ _POST哪一个是最快的?)
This is simpler than @lepe's solution.
这比@lepe 的解决方案更简单。
回答by Fusca Software
I just used post and an if:
我只是使用了 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!");
});
回答by Stan Bashtavenko
I had code that was doing getJSON. I simply replaced it with post. To my surprise, it worked
我有执行 getJSON 的代码。我只是用post替换了它。令我惊讶的是,它奏效了
$.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
}
回答by Tony
$.getJSON()
is pretty handy for sending an AJAX request and getting back JSON data as a response. Alas, the jQuery documentation lacks a sister function that should be named $.postJSON()
. Why not just use $.getJSON()
and be done with it? Well, perhaps you want to send a large amount of data or, in my case, IE7 just doesn't want to work properly with a GET request.
$.getJSON()
发送 AJAX 请求和返回 JSON 数据作为响应非常方便。唉,jQuery 文档缺少一个应该命名为$.postJSON()
. 为什么不直接使用$.getJSON()
并完成它?好吧,也许您想发送大量数据,或者就我而言,IE7 只是不想使用 GET 请求正常工作。
It is true, there is currently no $.postJSON()
method, but you can accomplish the same thing by specifying a fourth parameter (type) in the $.post()
function:
确实,目前没有$.postJSON()
方法,但你可以通过在$.post()
函数中指定第四个参数(类型)来完成同样的事情:
My code looked like this:
我的代码如下所示:
$.post('script.php', data, function(response) {
// Do something with the request
}, 'json');
回答by mic
if you have just two parameters you can do this:
如果你只有两个参数,你可以这样做:
$.getJSON('/url-you-are-posting-to',data,function(result){
//do something useful with returned result//
result.variable-in-result;
});