我现在正在将脚本更改为jquery.这是我的旧javascript:
var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : false); request.open("GET",url,true); request.onreadystatechange = updatePage; request.send(null); function updatePage() { if (request.readyState == 4) { if (request.status == 200) { var response = request.responseText; doSomething(); } else if (request.status == 304) { doSomethingElse(); } else { } } }
我现在想将其更改为jquery-ajax:
request = $.ajax({ type: "GET",url: url,data: data,success: updatePage });
如何测试请求返回的状态代码(和responseText),就像我在旧脚本中所做的那样?
解决方法
尝试这个:
request = $.ajax({ type: "GET",statusCode: { 200: function() { doSomething(); },304:function(){ doSomethingElse(); } } });
要么:
request = $.ajax({ type: "GET",complete: function(e,xhr,settings){ if(e.status === 200){ }else if(e.status === 304){ }else{ } } });