Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. if(window.XMLHttpRequest){
  2. xhr = new XMLHttpRequest();
  3. }
  4. else if(window.ActiveXObject){
  5. try{
  6. xhr = new ActiveXObject('Msxl2.XMLHTTP');
  7. } catch (e){}
  8. try{
  9. xhr = new ActiveXObject('Microsoft.XMLHTTP');
  10. } catch (e){}
  11. }
  12.  
  13. //var xhr = new XHR();
  14.  
  15. var json = JSON.stringify({
  16. os: jscd.os,
  17. user_agent:navigator.userAgent,
  18. referrer:jscd.referrer,
  19. current_url:jscd.current_url,
  20. hash:window.hash,
  21. utm_source:getParameterByName("utm_source"),
  22. utm_medium:getParameterByName("utm_medium"),
  23. utm_campaign:getParameterByName("utm_campaign"),
  24. utm_content:getParameterByName("utm_content"),
  25. utm_term:getParameterByName("utm_term")
  26. });
  27.  
  28. if(xhr){
  29.  
  30. xhr.open("POST", 'http://codes.seosoft.su/api/create', true);
  31.  
  32. xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
  33.  
  34. xhr.onreadystatechange = function() {
  35.  
  36. if (this.readyState != 4) return;
  37.  
  38. // по окончании запроса доступны:
  39. // status, statusText
  40. // responseText, responseXML (при content-type: text/xml)
  41.  
  42. if (this.status != 200) {
  43. // обработать ошибку
  44. //alert( 'ошибка: ' + (this.status ? this.statusText : 'запрос не удался') );
  45. return;
  46. }
  47.  
  48. //console.log(this);
  49.  
  50. // получить результат из this.responseText или this.responseXML
  51. if(this.statusText=="OK"){//get promo-code
  52.  
  53. request = JSON.parse(this.responseText);
  54.  
  55. //write in cookie
  56. setCookie("track", request.promo, 30);
  57.  
  58. document.getElementById('target-promo').innerHTML = request.promo;
  59. }
  60. //console.(log("responseText="+typeof (this.typeof responseText.promo)));
  61. //console.log("statusText="+this.statusText);
  62. };
  63.  
  64.  
  65. xhr.withCredentials = true;
  66.  
  67. // Отсылаем объект в формате JSON и с Content-Type application/json
  68. // Сервер должен уметь такой Content-Type принимать и раскодировать
  69. xhr.send(json);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement