zoukankan      html  css  js  c++  java
  • 客户端异步取服务端数据

    1.js可跨域

    convertServer={
    requestByScript: function(scriptId, dataHref, callback, charset, retry) {
            var isReady = false;
            if (callback) {
                if (typeof (callback) == "string") {
                    charset = callback;
                    callback = null;
                }
            }
            var head = document.getElementsByTagName("head")[0];
            var objScript = document.getElementById(scriptId);
            if (objScript && !document.all) {
                objScript.src = "";
                objScript.parentNode.removeChild(objScript);
                objScript = null;
            }
            if (objScript != null) {
                if (dataHref.indexOf("?") == -1) dataHref += "?";
                dataHref += "&" + Math.random();
                objScript.src = dataHref;
                var dataScript = objScript;
            } else {
                var dataScript = document.createElement("script");
                dataScript.id = scriptId;
                if (charset) {
                    dataScript.charset = charset;
                } else {
                    dataScript.charset = "GB2312";
                }
                dataScript.src = dataHref;
                dataScript.defer = true;
                dataScript.type = "text/javascript";
                head.appendChild(dataScript);
            }
            if (document.all) {
                dataScript.onreadystatechange = function() {
                    if (dataScript.readyState == "loaded" || dataScript.readyState == "complete") {
                        isReady = true;
                        if (callback) callback();
                    }
                }
            } else {
                dataScript.onload = function() {
                    isReady = true;
                    if (callback) callback();
                }
            }

            if (retry) {
                setTimeout(function() {
                    if (retry.times > 0 && !isReady) {
                        retry.times--;
                        if (dataHref.indexOf("?") == -1) dataHref += "?";
                        dataHref += "&" + Math.random();
                        convertServer.requestByScript(scriptId, dataHref, callback, charset, retry);
                    }
                }, retry.timeout);
            }
        },
        requestData:function(url,callBack)
        {
            var scriptId="jsReuest"+ Math.random();            
            convertServer.requestByScript(scriptId, url, callBack)         
        }

    }

    2. jQuery.ajax(options),不能跨域

  • 相关阅读:
    微软教程:ASP.NET Core MVC 入门
    微软教程:ASP.NET Core Razor Pages 入门
    微软教程:ASP.NET Core Web API 入门
    微软教程:ASP.NET Core SignalR 入门(实时Web应用)
    Entity Framework Core 系列教程(翻译)
    视频教程:ASP.NET Core 3.x 构建 RESTful API(高级)
    C#(99):C# 9.0 新特性( NET Framework 5.0 与 Visual Studio ? )
    视频教程:VS Core 40分钟进行WebAPI开发和调用(入门级别)
    视频教程:ASP.NET Core 3.x 入门(包括MVC、Razor Page、Blazor、SignalR、gRPC)
    EntityFramework Core入门教程-12-在ASP.NET Core项目中配置EF Core
  • 原文地址:https://www.cnblogs.com/ylemzhang/p/1836028.html
Copyright © 2011-2022 走看看