Cyr*_*pta 8 javascript asp.net-mvc jquery json
首先我的代码
$.getJSON("./posts/vote/" + postId + "/1", null, function(result) {
if (result.result == true)
$("#pst" + postId + " > .pstside > .rank > .score").html(result.voteCount);
});
Run Code Online (Sandbox Code Playgroud)
我有一堆按钮,每个按钮带有代码,从ASP.Net MVC控制器动作中带来一些投票结果.
这在第一次单击按钮时效果很好,但如果再次单击该按钮则没有任何反应.什么都没有到达服务器.
我在FireFox上测试.
出了什么问题?某种奇怪的缓存?因为请求第二次永远不会到达我的控制器,所以javascript似乎执行正常但仍然返回旧值.
CMS*_*CMS 18
听起来像一个浏览器的缓存问题(如果是,我敢肯定,与IE发生),你可能需要使用$阿贾克斯和缓存选项设置为false,因为它仅适用于数据类型是默认为false script和jsonp:
$.ajax({
type: "GET",
url: "./posts/vote/" + postId + "/1",
success: function (result) {
if (result.result == true)
$("#pst" + postId + " > .pstside > .rank > .score").html(result.voteCount);
},
dataType: "json",
cache: false
});
Run Code Online (Sandbox Code Playgroud)
或者你可以在使用$ .getJSON之前使用$ .ajaxSetup 为所有jQuery Ajax函数全局设置该选项:
$.ajaxSetup({ cache: false });
Run Code Online (Sandbox Code Playgroud)
编辑:您可以执行POST请求返回JSON,如下所示:
$.post("./posts/vote/" + postId + "/1",
function (result) {
if (result.result == true)
$("#pst" + postId + " > .pstside > .rank > .score").html(result.voteCount);
}, "json");
Run Code Online (Sandbox Code Playgroud)
如果您计划执行大量postJSON请求,您可以创建自己的函数:
jQuery.postJSON = function(url, data, callback) {
jQuery.post(url, data, callback, "json") ;
};
Run Code Online (Sandbox Code Playgroud)
并且您将能够像$ .getJSON一样使用它