2012-07-28 56 views
0

我有一個js文件中的代碼,我將其包含在默認頁面中,以在客戶端的瀏覽器中創建cookie,並在Thankyou頁面中使用它來調用我的web服務來跟蹤支付交易。使用JQuery調用WebService不適用於所有瀏覽器,但僅適用於IE9?

// Read a page's GET URL variables and return them as an associative array.

$(document).ready(function() {

$.extend({ 
    getUrlVars: function() { 
     var vars = [], hash; 
     var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 
     for (var i = 0; i < hashes.length; i++) { 
      hash = hashes[i].split('='); 
      vars.push(hash[0]); 
      vars[hash[0]] = hash[1]; 
     } 
     return vars; 
    }, 
    getUrlVar: function (name) { 
     return $.getUrlVars()[name]; 
    }, 
    getCookie: function (name) { 
     var dc = document.cookie; 
     var prefix = name + "="; 
     var begin = dc.indexOf("; " + prefix); 
     if (begin == -1) { 
      begin = dc.indexOf(prefix); 
      if (begin != 0) return null; 
     } 
     else { 
      begin += 2; 
      var end = document.cookie.indexOf(";", begin); 
      if (end == -1) { 
       end = dc.length; 
      } 
     } 
     return unescape(dc.substring(begin + prefix.length, end)); 
    } 
}); 


{ 
    var cookieStart, cookieEnd, cookieValue, UserGuid, productId, AffiliationURL, PricePerUnit, commissionAmount; 

    // Get object of URL parameters 
    if ($.getUrlVars() != null) { 
     var allVars = $.getUrlVars(); 
    } 

    // Getting URL var by its name 

    //Now check if the user is from Seek Site?? 
    //If this is not null that means the user is Refered from Seek Site 
    if ($.getUrlVar('clientId') != null) { 

     UserGuid = $.getUrlVar('clientId'); 

     if ($.getUrlVar('productId') != null) { 
      productId = $.getUrlVar('productId'); 
     } 
     if ($.getUrlVar('AffiliationURL') != null) { 
      AffiliationURL = $.getUrlVar('AffiliationURL'); 
     } 
     if ($.getUrlVar('PricePerUnit') != null) { 
      PricePerUnit = $.getUrlVar('PricePerUnit'); 
     } 
     if ($.getUrlVar('commissionAmount') != null) { 
      commissionAmount = $.getUrlVar('commissionAmount'); 
     } 


     //Now Create the cookie for the user 
     var myCookie = $.getCookie("ReferedCookie"); 
     alert(myCookie); 
     if (myCookie != null) { 
      // cookie exists 
      cookieStart = myCookie.indexOf("clientId="); 
      //alert(cookieStart = cookieStart + "ReferedCookie=".length); 
      cookieEnd = myCookie.indexOf(";", cookieStart); 

      //if there is no occurence of the semicolon character 
      //cookieEnd takes the length of the document.cookie property 
      if (cookieEnd == -1) cookieEnd = myCookie.length; 

      cookieValue = myCookie.substring(cookieStart, cookieEnd); 



      // check the Product Id 
      if (cookieValue.indexOf(productId + "&", "productId=") != -1) { 
       // that means the User clicked on the same Product again and there is already a cookie Exists for that product 
       alert("User clicked on the same Product again"); 
      } 

      else { 
       // The Product Id is different ,We are going to add that product value as string to the cookie 

      } 
     } 

     else { 
      // Create Cookie 
      var expiryDate = new Date(); 
      expiryDate.setTime(expiryDate.setDate(expiryDate.getDate() + 365)); // 365 days 

      document.cookie = "ReferedCookie=" + "clientId=" + UserGuid + "&productId=" + productId + "&AffiliationURL=" + AffiliationURL + "&PricePerUnit=" + PricePerUnit + "&commissionAmount=" + commissionAmount + ";" + "expires=" + expiryDate.toGMTString() + ";"; 

     } 

    } 
}}); 

這裏的代碼,我想在三江源頁面運行,但它在IE9運行(在時間我硝基甲苯知道爲什麼???)

{ 
var cookieStart, cookieEnd, cookieValue, UserGuid, productId, AffiliationURL, PricePerUnit, commissionAmount; 
$(window).load(function (e) { 
    e.preventDefault(); 
    $.extend({ 
     readCookie: function (name) { 
      var nameEQ = name + "="; 
      var ca = document.cookie.split(';'); 
      for (var i = 0; i < ca.length; i++) { 
       var c = ca[i]; 
       while (c.charAt(0) == ' ') c = c.substring(1, c.length); 
       if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); 
      } 
      return null; 
     } 
    }); 

    var x = new Array(3); 

    cookieValue = $.readCookie("ReferedCookie"); 

    var s = 0; 
    var pos = cookieValue.indexOf("&"); 
    while (pos > -1) { 
     x[s] = pos; 
     pos = cookieValue.indexOf("&", pos + 1); 
     // alert(x[s]); 
     s++; 
    } 

    var c1 = cookieValue.indexOf("clientId="); 
    alert(UserGuid = cookieValue.substring(c1 + 9, x[0])); 

    var p1 = cookieValue.indexOf("productId="); 
    alert(productId = cookieValue.substring(p1 + 10, x[1])); 

    var A1 = cookieValue.indexOf("AffiliationURL="); 
    alert(AffiliationURL = cookieValue.substring(A1 + 15, x[2])); 

    var pp1 = cookieValue.indexOf("PricePerUnit="); 
    alert(PricePerUnit = cookieValue.substring(pp1 + 13, x[3])); 

    var com1 = cookieValue.indexOf("commissionAmount="); 
    alert(commissionAmount = cookieValue.substring(com1 + 17)); 


    var ServiceURL = 'http://localhost:12445/Service/TrackPayment.asmx/InsertCommissionRecord'; 
    // var d = '{"ProductID": "' + productId + '" , "AffiliationURL": "' + AffiliationURL + '" , "Quantitiy": "' + 15 + '" , "PricePerUnit": "' + PricePerUnit + '" , "commissionAmount": "' + commissionAmount + '"}'; 
    var d = '{"ProductID":"1","AffiliationURL":"1","Quantitiy":"1","PricePerUnit":"1","commissionAmount":"1"}'; 
    alert(d); 
    $.ajax({ 
     type: 'POST', 
     data: d, 
     url: ServiceURL, 
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json', 
     success: 
      function (data, textStatus, XMLHttpRequest) { 
       alert(data); 
       alert(textStatus); 

       alert(XMLHttpRequest); 
      }, 
     error: 
      function (XMLHttpRequest, textStatus, errorThrown) { 
       alert(textStatus); 
       alert(errorThrown); 
      } 
    }); 
});} 

在螢火蟲控制檯它顯示data.d爲空

請h請讓我出去,請指出我出錯的地方。 感謝

+0

使用Jquery.cookie插件 – Kir 2012-07-28 11:06:30

+0

的插件網站目前正在開發中。 「http://archive.plugins.jquery.com/project/Cookie」 – 2012-07-28 11:20:32

+1

可能是這樣的:https://github.com/carhartl/jquery-cookie/ – Kir 2012-07-28 11:23:35

回答

0

JS:

$.ajax({ 
    type: 'POST', 
    data: {'d': d}, 
    url: ServiceURL, 
    dataType: 'json', 
    success: 
     function (data, textStatus, XMLHttpRequest) { 
      alert(data.ProductID); // returns '1' 
     }, 
    error: 
     function (XMLHttpRequest, textStatus, errorThrown) { 
      alert(textStatus); 
      alert(errorThrown); 
     } 
}); 

test.php的:

$data = json_decode($_POST['d']); 
echo json_encode($data); 
+0

的成功函數裏面作爲php的一個變種:'$ data =(array)json_decode($ _ POST ['d']);' – Kir 2012-07-29 14:41:59

相關問題