var xmlHttp; // 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; } if (method == "POST") { xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } return xhr; } // Helper method to parse the title tag from the response. function getTitle(text) { return text.match(‘<title>(.*)?</title>‘)[1]; } //function callback() { // if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { // var b = xmlHttp.responseText; // alert(b); // } //} // Make the actual CORS request. function makeCorsRequest(url) { // All HTML5 Rocks properties support CORS. xmlHttp = createCORSRequest(‘POST‘, url, true); //xmlHttp.onreadystatechange = callback; if (!xmlHttp) { alert(‘CORS not supported‘); return; } // Response handlers. xmlHttp.onload = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { var text = xmlHttp.responseText; // var title = getTitle(text); var resultDiv = document.getElementById("callbacktext"); resultDiv.innerHTML = text; //alert(‘Response from CORS request to ‘ + text); } }; xmlHttp.onerror = function () { alert(‘Woops, there was an error making the request.‘); }; var params = "Email=" + $("#email").val(); xmlHttp.send(params); } function urlDeal(url){ //解决缓存的转换 if (url.indexOf("?") >= 0) { url = url + "&t=" + (new Date()).valueOf(); } else { url = url + "?+=" + (new Date()).valueOf(); } //解决跨域的问题 if (url.indexOf("http://") >= 0) { url.replace("?", "&"); url = "Proxy?url=" + url; } return url; } var url = "请求的API地址"; //var params = { "Email": "[email protected]" }; $("#btnPost").delegate("", "click", function () { makeCorsRequest(url); });
时间: 2024-10-27 11:03:42