HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了html5使得CORS更简单大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

以前使用CORS时比较麻烦,浏览器各种设置

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<script type="text/javascript" src="./file.js"></script>
</head>
<body>
<script type="text/javascript">
	// Create the XHR object.
function createCORSrequest(method,url) {
   var xhr = new XMLhttprequest();
   if ("withCredentials" in xhr) {
      // XHR for Chrome/Firefox/Opera/Safari.
      xhr.open(method,url,truE);
   }else if (typeof XDomainrequest != "undefined") {
      // XDomainrequest for IE.
      xhr = new XDomainrequest();
      xhr.open(method,url);
   }else {
      // CORS not supported.
      xhr = null;
   }
   return xhr;
}

// Helper method to parse the title tag from the response.
function gettitle(text) {
   return text.match('<title>(.*)?</title>')[1];
}

// Make the actual CORS request.
function makeCorsrequest() {
   
   // All HTML5 Rocks properties support CORs.
   var url = 'XXX';  
   var xhr = createCORSrequest('GET',url);  
   if (!xhr) {
      alert('CORS not supported');
      return;
   }
   
   // Response handlers.
   xhr.onload = function() {
      var text = xhr.responseText;
      var title = gettitle(text);
      alert('Response from CORS request to ' + url + ': ' + titlE);
   };
   
   xhr.onerror = function() {
      alert('Woops,there was an error making the request.');
   };
   xhr.send();
}
</script>

<button onclick="javascript:makeCorsrequest()">Button</button>
</body>
</html>

大佬总结

以上是大佬教程为你收集整理的html5使得CORS更简单全部内容,希望文章能够帮你解决html5使得CORS更简单所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。