jQuery的AJAX生产304响应当它不应该 [英] jQuery AJAX producing 304 responses when it shouldn't

查看:108
本文介绍了jQuery的AJAX生产304响应当它不应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的有我抓我的头。也就是因为这只是发生在IE浏览器,而不是Firefox和我下的IM pression说:jQuery是有效的浏览器中性。我一直在龟裂这件事情在过去的几个小时,已经确定下来,至少发生了什么。

This really has me scratching my head. Namely because it only happens in IE, not Firefox, and I was under the impression that jQuery was effectively browser neutral. I've been cracking at this thing for the past few hours and have nailed down, at least, what is happening.

这jqGrid的:

$("#DocumentListByPartRecordsGrid").jqGrid(
          {
            datatype: 'local',            
            colNames: ['<b>Id</b>', '<b>Document Name</b>', '<b>Document Type</b>', '<b>Effective Date</b>', '<b>Expiration Date</b>', '<b>Delete</b>'],
            colModel: [
                  { name: 'ASSOCIATION_ID', Index: 'ASSOCIATION_ID', resizable: true, align: 'left', hidden: true, sortable: false },
                  { name: 'FILE_NAME', Index: 'FILE_NAME', resizable: true, align: 'left', sortable: false, width:'20%' },
                  { name: 'DOCUMENT_TYPE', Index: 'DOCUMENT_TYPE', resizable: true, align: 'left', sortable: false, width:'20%' },
                  { name: 'EFFECTIVE_DATE', Index: 'EFFECTIVE_DATE', resizable: true, align: 'left', sortable: false, width:'20%' },
                  { name: 'EXPIRATION_DATE', Index: 'EXPIRATION_DATE', resizable: true, align: 'left', sortable: false, width:'20%' },
                  { name: 'Delete', Index: 'Delete',resizable: true, align: 'center', sortable: false, width:'20%' },
                  ],            
            rowNum: 15,
            rowList: [15, 50, 100],
            imgpath: '/Drm/Content/jqGrid/steel/images',
            viewrecords: true,            
            height: 162,           
            loadui: 'block',
            forceFit: true
        });

通过此功能填充:

Filled by this function:

var mydata = '';    
<% if(!string.IsNullOrEmpty(Model.PCAssociatedDocuments)) { %>        
   var mydata = <%= Model.PCAssociatedDocuments %>;
<% } %>

for (var i = 0; i <= mydata.length; i++){
        jQuery("#DocumentListByPartRecordsGrid").addRowData(i, mydata[i], "last");
        }

整齐的填充的模型。这不是问题。使用删除功能,这是格式化回到控制器一样,所以当问题出现了:

Which is cleanly populated from the model. This is not the issue. The issue arises when using the delete functionality, which is formatted back in the controller like so:

<a class='deleteAttachment' style='cursor: pointer;' href='#' onclick='javascript:PCDocumentDelete(" + s.AssociationId.ToString() + ", " + pcId + ");'>Delete</a>

和调用这个函数

function PCDocumentDelete(id, pcid) {
if (confirm("Are you sure you want to delete this document?")) {
    $.blockUI({
        message: "Working...",
        css: {
            background: '#e7f2f7',
            padding: 10
        }
    });
    $.ajax(
        {
            url: '/DRM/Pc/DeleteAssociation?associationId=' + id + '&pcid=' + pcid,
            async: true,
            dataType: "json",
            success: function(result) {
                if (result.Success == true) {
                    //Reload grid                       
                    $.ajax({ async: false });
                    $("#DocumentListByPartRecordsGrid").setGridParam({ url: "/Drm/Pc/DeAssociatePartRecordsWithDocument?pcid=" + pcid, datatype: 'json', myType: 'GET', page: 1 });
                    $("#DocumentListByPartRecordsGrid").trigger("reloadGrid");
                    $.unblockUI();
                    $.showGlobalMessage('Specified document has been successfully disassociated from this part record.');
                }
                else {
                    $.unblockUI();
                    $.showGlobalMessage('An error occurred deleting the attachment.');
                }
            },
            error: function(res, stat) {
                alert(res.toString());
                alert(stat.toString());
            }
        });
    return false;
}
else {
    return false;
}

}

(showGlobalMessage是一个内部函数,用于创建一个特殊格式的blockUI)

(showGlobalMessage is an internal function that creates a particularly formatted blockUI)

Ajax调用的方法早在控制器,但出现的问题,我们做之前那么远,所以,除非有人认为它很重要,我没有要发布的code。什么情况是,经常莫名其妙的原因,第一个突发的Ajax调用PC / DeleteAssociation是回来了以304(未修改)响应。我知道,发生在一个GET时,一切都没有改变,需要被刷新。但是,这不是一个GET,它应该被视为一个职位,我下了jquery.ajax的目的是,除非有其他说明,不能生成304响应的IM pression。我显然失去了一些东西,并一直盯着它太长时间,以赶上它自己。任何人都明白我错过了什么?谢谢你。

The ajax calls a method back in the controller, but the issue arises before we make it that far, so unless someone thinks it important, I'm not going to post that code. What happens is, often for inexplicable reasons, the first burst of ajax that calls PC/DeleteAssociation is coming back with a 304 (not modified) response. I know that happens on a get when nothing has changed that needs to be refreshed. But this isn't a get, it should be treated as a post, and I was under the impression that jquery.ajax was designed to, unless otherwise instructed, not generate 304 responses. I'm obviously missing something here and have been staring at it far too long to catch it myself. Anyone see what I missed? Thank you.

推荐答案

我不明白,你指定Ajax请求为POST。所以基本上地址:

I cannot see, you specifying the ajax request as a POST. So basically add:

$.ajax({ type: 'POST' });

如果仍然失败(由于一些浏览器的AJAX的怪事),你可以尝试设置缓存:假

$.ajax({ type: 'POST', cache: false });

顺便说一句,所有的缓存:假的呢,是加入了一些随机的东西到请求的URL

Btw, all cache: false does, is adding some random stuff to the request URL.

EDIT1:

关于

......,我是在IM pression的   jquery.ajax被设计为,除非   另有指示,不产生304   响应

... and I was under the impression that jquery.ajax was designed to, unless otherwise instructed, not generate 304 responses

jQuery的istn't这里产生任何响应。而304头仅仅是一个HTTP头。 HTTP AJAX请求是普通的HTTP请求,并可能返回任何有效的标题。如果服务器304响应,XHR对象将只是充当了来自服务器的本地缓存的响应。这是完全对用户透明,但。

jQuery istn't generating any responses here. And the 304-header is just an HTTP header. HTTP AJAX requests are ordinary HTTP requests and may return any valid header. If the server responds with 304, the XHR object will simply serve up the locally cached response from the server. It's completely transparent for the user, though.

EDIT2:

删除约preventing缓存的建议。好像巫毒教给我。

Removed the advice about preventing caching. Seems like Voodoo to me.

EDIT3:

补充说明一点再次,因为它显然是必要的。展望各地的网络,IE浏览器似乎被非法缓存AJAX职位,以在一定程度上。

Added that bit in again because it apparently was necessary. Looking around the web, IE seems to be illegally caching AJAX POSTs to some extent.

这篇关于jQuery的AJAX生产304响应当它不应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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