1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| function sendData(data) { var XHR = new XMLHttpRequest(); var urlEncodedData = ""; var urlEncodedDataPairs = []; var name;
for (name in data) { urlEncodedDataPairs.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name])); }
urlEncodedData = urlEncodedDataPairs.join('&').replace(/%20/g, '+');
XHR.addEventListener('load', function (event) { alert('发送成功'); } );
XHR.addEventListener('error', function (event) { alert('发送失败'); } );
XHR.open('POST', 'https://example.com/cors.php');
XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
XHR.send(urlEncodedData); }
|