network_err xmlHttpRequestException 101 解决方案

本文介绍了解决手机客户端内WebView页面加载时遇到network_err xmlHttpRequestException 101错误的方法。主要包括两种方案:一是将DWR框架替换为Ajax进行数据交换;二是通过设置document.domain来解决跨域问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手机客户端请求webview 网页,出现
network_err xmlHttpRequestException 101
 
解决方案一、
 
如果是手机客户端嵌套的WebView 页面
这时候检查你的jsp 页面里面是否用到异步交互的框架。例如DWR框架。
如果用到DWR框架,DWR内部设定的安全机制,不允许跨域的操作。这是候你可以将Dwr框架访问后台数据的方式改变一下。
将DWR代码改成 用Ajax 提交
 
jsp代码:
 if (Trim(Email) != "") {
   var action = "user!doCheckEmailIsExists.php";
   $.ajax({ 
    type:"get", 
    data: { 
     email : Email 
    }, 
     url:action
    dataType:"jsonp", 
    jsonp:"callback", 
    success:function(data){
     if(data=="0"){
      document.forms[0].action = "user!UserEmailReg.php";
      document.forms[0].submit();
     }else{
      $("#Email").focus();
      alert("此邮箱已被使用。您可以1.使用其他邮箱注册;2.用此邮箱登录或找回密码。如有问题请联系杏树林客服(010-5247-9170)");
      return false;
     }
    } 
   });
  }
 
 
java Action代码

/**
  * 判断邮箱是否存在
  */
 public void doCheckEmailIsExists() {
  request = ServletActionContext.getRequest();
  response = ServletActionContext.getResponse();

  String result = "";
  response.setContentType("text/html; charset=utf-8");
  response.setHeader("Pragma", "No-cache");
  response.setDateHeader("Expires", 0);
  response.setHeader("Cache-Control", "no-cache");
  PrintWriter out = null;
  
  String callback = request.getParameter("callback");
  String email = request.getParameter("email");
  System.out.print(callback);
  
  try {
   out = response.getWriter();
   if (!"".equals(email) && email != null) {
    User_Condition condition = new User_Condition();
    condition.setEmail(email);
    condition.setAuthenticateemail(true);
    User mUser = WebManage.userCenter_DataAccess.invoke_User
      .GetObjectByCondition(condition);
    if (mUser != null) {
     userNameExist = "1";

    } else {
     userNameExist = "0";
    }
   } else {
    userNameExist = "2";
   }
   result = callback+"("+userNameExist+")";
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   out.print(result);
   out.flush();
   out.close();
  }

 }

 
 
 
 
 
 
 
 
 

解决方案二、

document.domain="xingshulin.com";

以下是说明:

因为浏览器的安全策略,浏览器不允许不同域(比如:dancewithnet.com和lab.dancewithnet.com)、不同协议(比如:http://dancewithnet.com和https://dancewithnet.com)、不同端口(比如:http:dancewithnet.com和http://dancewithnet.com:8080)下的页面通过XMLHTTPRequest相互访问,这个问题同样影响着不同页面的Javascript的相互调用和控制,但是当主域、协议、端口相同时,通过设置页面的document.domain主域,Javascript可以在不同的子域名间访问控制,比如通过设置document.domain=’dancewithnet.com’,http://dancewithnet.com和http://lab.dancewithnet.com页面可互访,这个特性也提供了此情况下不同子域名下的XMLHTTPRequest相互访问的解决方案。

对于主域、协议、端口相同时的Ajax跨域问题,很早就有设置document.domain来解决的说法,但一直没有看到具体的成功应用,这几天尝试了一下,其原理就是,利用一个隐藏的iframe引入所跨另一子域的页面作为代理,通过Javascript来控制iframe引入的另一子域的XMLHTTPRequest来进行数据获取。请看实例>>

对于不同主域/不同协议/不同端口下的Ajax访问需要通过后台的代理来实现,更多细节可以看看Use a Web Proxy for Cross-Domain XMLHttpRequest CallsFixing AJAX: XMLHttpRequest Considered Harmful

 
 
前端有问题后端在// 验证版本号 int currentVersion = versionService.getVersion(username); if (tokenVersion != currentVersion) { log.warn("令牌版本不匹配: {} != {}", tokenVersion, currentVersion); return ResponseEntity.status(498).build(); // 自定义失效状态码 }返回以后前端弹出视频加载失败: MEDIA_ERR_SRC_NOT_SUPPORTED - 不支持的视频格式,按道理来说我们在这里应该重新请求令牌,设置视频源跳转到指定进度位置正常播放视频的,前端我是根据你提供的代码修改的,你提供的前端业务代码:$(function () { // ...(原有变量声明和初始化保持不变)... // 新增状态变量 let currentPosition = 0; // 当前播放位置(毫秒) let isSeeking = false; // 是否正在跳转中 // 获取视频访问令牌(不包含位置参数) function fetchVideoToken() { $ loadingOverlay.show(); $ tokenStatus.text('获取中...').removeClass('bg-success bg-danger').addClass('bg-warning'); return new Promise((resolve, reject) => { $.ajax({ url: `${config.apiBaseUrl}/token`, method: 'POST', dataType: 'json', contentType: 'application/json', data: JSON.stringify({ videoId: config.videoId }), // 不包含位置参数 }) .done(function (res) { isPremium = res.isPremium; localStorage.setItem('isPremium', res.isPremium); $ tokenStatus.text('已获取').removeClass('bg-warning').addClass('bg-success'); resolve(res.token); }) .fail(reject); }); } // 加载视频流(不包含位置参数) function loadVideoStream(token) { resetVideoState(); $ loadingOverlay.show(); // 构造视频流URL(不包含位置参数) const streamUrl = `${config.apiBaseUrl}/play/${config.videoId}?token=${encodeURIComponent(token)}`; setVideoSource($ video, streamUrl); } // 安全跳转处理(核心机制保留) $ video[0].addEventListener('seeking', async function() { if (isSeeking) return; isSeeking = true; // 记录跳转目标位置 const targetPosition = this.currentTime; try { // 刷新令牌 const newToken = await fetchVideoToken(); // 保存当前播放状态 const wasPlaying = !this.paused; const currentVolume = this.volume; // 重新加载视频流 loadVideoStream(newToken); // 新视频加载完成后跳转到目标位置 $ video.on('loadeddata', function handler() { $ video.off('loadeddata', handler); $ video[0].currentTime = targetPosition; $ video[0].volume = currentVolume; if (wasPlaying) attemptPlay($ video); isSeeking = false; }); } catch (error) { console.error('跳转失败:', error); // 回退到跳转前位置 $ video[0].currentTime = currentPosition; isSeeking = false; } }); // 初始化播放位置追踪 $ video.on('timeupdate', function() { currentPosition = this.currentTime; }); // 自定义错误处理(保留令牌刷新机制) $ video.on('error', function(e) { const errorCode = e.target.error?.code; // 令牌失效错误处理 if (errorCode === 498) { fetchVideoToken().then(newToken => { loadVideoStream(newToken); $ video[0].currentTime = currentPosition; $ video[0].play(); }); } // ...其他错误处理保持不变... }); // 会员状态变更处理(保留) window.addEventListener('membershipChanged', () => { fetchVideoToken().then(newToken => { loadVideoStream(newToken); $ video[0].currentTime = currentPosition; $ video[0].play(); }); }); });
最新发布
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值