Ajax 和 jQuery 页面重新加载
Ajax and jQuery page reloading
我在制作功能时遇到问题 - 在不重新加载的情况下加载页面,我遇到的问题是 "I want to show alert when link is 404 or not net connection is lost"。我知道此代码 jqXHR.status == 0
获取网络连接错误或 jqXHR.status !== 0
404 但我不知道我不知道如何使用现有代码实现此代码。
请帮我写这个函数
$(function(){
$("a[rel='tab']").click(function(e){
pageurl = $(this).attr('href');
$.ajax({url:pageurl+'?rel=tab',success: function(data){
$('#containt').html(data);
}});
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
return false;
});
});
$(window).bind('popstate', function() {
$.ajax({url:location.pathname+'?rel=tab',success: function(data){
$('#containt').html(data);
}});
});
<a rel="tab" href="./404.php">Show me 404 ALert Pls</a>
为“错误”事件或“statusCode”添加回调。
$.ajax({
url:pageurl+'?rel=tab',
success: function(data){
$('#containt').html(data);
},
statusCode: {
404: function() {
alert( "page not found" );
}
}
});
来自docs:
错误
Type: Function( jqXHR jqXHR, String textStatus, String
errorThrown )
A function to be called if the request fails. The
function receives three arguments: The jqXHR (in jQuery 1.4.x,
XMLHttpRequest) object, a string describing the type of error that
occurred and an optional exception object, if one occurred. Possible
values for the second argument (besides null) are "timeout", "error",
"abort", and "parsererror". When an HTTP error occurs, errorThrown
receives the textual portion of the HTTP status, such as "Not Found"
or "Internal Server Error." (in HTTP/2 it may instead be an empty
string) As of jQuery 1.5, the error setting can accept an array of
functions. Each function will be called in turn. Note: This handler is
not called for cross-domain script and cross-domain JSONP requests.
This is an Ajax Event.
statusCode(默认:{})
Type: PlainObject An object of numeric HTTP codes and functions to be
called when the response has the corresponding code. If the request is
successful, the status code functions take the same parameters as the
success callback; if it results in an error (including 3xx redirect),
they take the same parameters as the error callback.
(version added: 1.5)
您可以使用错误函数以这种方式完成
$.ajax({
url: pageurl + '?rel=tab',
error: function (xhr, ajaxOptions, thrownError) {
if(xhr.status == 0){
alert('connect to internet');
return false; }
alert(xhr.status);
// here you can use the xhr code to return to main page or whatever
alert(thrownError);
},
success: function (data) {
$('#containt').html(data);
}
});
我在制作功能时遇到问题 - 在不重新加载的情况下加载页面,我遇到的问题是 "I want to show alert when link is 404 or not net connection is lost"。我知道此代码 jqXHR.status == 0
获取网络连接错误或 jqXHR.status !== 0
404 但我不知道我不知道如何使用现有代码实现此代码。
请帮我写这个函数
$(function(){
$("a[rel='tab']").click(function(e){
pageurl = $(this).attr('href');
$.ajax({url:pageurl+'?rel=tab',success: function(data){
$('#containt').html(data);
}});
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
return false;
});
});
$(window).bind('popstate', function() {
$.ajax({url:location.pathname+'?rel=tab',success: function(data){
$('#containt').html(data);
}});
});
<a rel="tab" href="./404.php">Show me 404 ALert Pls</a>
为“错误”事件或“statusCode”添加回调。
$.ajax({
url:pageurl+'?rel=tab',
success: function(data){
$('#containt').html(data);
},
statusCode: {
404: function() {
alert( "page not found" );
}
}
});
来自docs:
错误
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." (in HTTP/2 it may instead be an empty string) As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.
statusCode(默认:{})
Type: PlainObject An object of numeric HTTP codes and functions to be called when the response has the corresponding code. If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback.
(version added: 1.5)
您可以使用错误函数以这种方式完成
$.ajax({
url: pageurl + '?rel=tab',
error: function (xhr, ajaxOptions, thrownError) {
if(xhr.status == 0){
alert('connect to internet');
return false; }
alert(xhr.status);
// here you can use the xhr code to return to main page or whatever
alert(thrownError);
},
success: function (data) {
$('#containt').html(data);
}
});