xhr.tag

来自「ajax基础操作实例,无刷添加删除等..」· TAG 代码 · 共 56 行

TAG
56
字号
<%@ attribute name="function" required="true" rtexprvalue="true" %>
<%@ attribute name="method" required="false" rtexprvalue="true" %>
<%@ attribute name="url" required="true" rtexprvalue="true" %>
<%@ attribute name="sync" required="false" rtexprvalue="true"
    type="java.lang.Boolean" %>
<%@ attribute name="json" required="false" rtexprvalue="true"
    type="java.lang.Boolean" %>
<%@ attribute name="cpr" required="false" rtexprvalue="true"
    type="java.lang.Boolean" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="dau" tagdir="/WEB-INF/tags/dynamic/ajax/util" %>

<c:set var="method" value="${empty method ? 'GET' : fn:toUpperCase(method)}"/>
<c:set var="reqVarName"
    value="${fn:trim(fn:substringBefore(function, '('))}Request"/>

<c:if test="${cpr}">var ${reqVarName} = null;</c:if>

function ${function} {
    <c:if test="${cpr}">if (${reqVarName}) closeRequest(${reqVarName});</c:if>

    var url = "${url}";
    <c:if test="${method == 'GET'}">
        url += '?';
        <dau:appendParams jsVarName="url" function="${function}"/>
    </c:if>
    <c:if test="${method == 'POST'}">
        var body = "";
        <dau:appendParams jsVarName="body" function="${function}"/>
    </c:if>

    var request = openRequest("${method}", url, ${!sync});
    <c:if test="${cpr}">${reqVarName} = request;</c:if>
    if (request == null) return null;

    function processResponse() {
        if (request.readyState == 4) {
            if (request.status == 200) {
                <c:if test="${json}">eval(request.responseText);</c:if>
                <jsp:doBody/>
            } else {
                httpError(request);
                <c:if test="${method == 'POST'}">url += '?' + body;</c:if>
                document.location = url;
            }
        }
    }

    request.onreadystatechange = processResponse;
    <c:if test="${method == 'GET'}">sendRequest(request, null);</c:if>
    <c:if test="${method == 'POST'}">sendRequest(request, body);</c:if>
    return request;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?