歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> jQuery中的getJSON方法請求的接口有錯誤時的處理方法

jQuery中的getJSON方法請求的接口有錯誤時的處理方法

日期:2017/3/1 10:33:35   编辑:Linux編程

在用jQuery中的getJSON方法時,通常會碰到請求的接口有錯誤的情況發生,這時我們可以用下面的方法來做處理,代碼如下:

<script type="text/javascript">

function GetDatas(url) {
try {
$.ajaxSetup({
error: function (x, e) {
$("#content").html("暫無具體內容!");
return false;
}
});

$.getJSON(url, function (data) {
if (data != null) {
$(".TitleStyle").html(data["root"][0]["data"][0]["Title"]);
var date = data["root"][0]["data"][0]["Date"];
if (date.length == 8) {
$("#strDate").html(date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8));
}
$("#strAuthor").html(data["root"][0]["data"][0]["SecuName"]);
$("#content").html(data["root"][0]["data"][0]["Content"]);
}
else {
$("#content").html("暫無具體內容!");
}
});
}
catch (ex) {
$("#content").html("暫無具體內容!");
}
}
</script>

Copyright © Linux教程網 All Rights Reserved