Guest User

Untitled

a guest
Mar 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. function createCORSRequest(method, url) {
  2. var username ='usuario';
  3. var password ='contraseña';
  4. var xhr = new XMLHttpRequest();
  5. xhr.setRequestHeader("Content-type", "text/xml");
  6. xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
  7. xhr.setRequestHeader("Ocp-Apim-Subscription-Key", 'api key');
  8. if ("withCredentials" in xhr) {
  9. xhr.open(method, url, true);
  10. } else if (typeof XDomainRequest != "undefined") {
  11. xhr = new XDomainRequest();
  12. xhr.open(method, url);
  13. } else {
  14. xhr = null;
  15. }
  16. return xhr;
  17. }
  18. function getTitle(text) {
  19. return text.match('<title>(.*)?</title>')[1];
  20. }
  21. function makeCorsRequest() {
  22. var url = "url";
  23. var method ="POST";
  24. var xhr = createCORSRequest(method, url);
  25. if (!xhr) {
  26. alert('CORS not supported');
  27. return;
  28. }
  29. var params = `xml a enviar`;
  30. xhr.onload = function() {
  31. var text = xhr.responseText;
  32. var title = getTitle(text);
  33. alert('Response from CORS request to ' + url + ': ' + title);
  34. };
  35. xhr.onerror = function() {
  36. alert('Woops, there was an error making the request.');
  37. };
  38. xhr.send(params);
  39. }
  40. makeCorsRequest();
Add Comment
Please, Sign In to add comment