0

このコードは、クライアントのブラウザで Cookie を作成するためのデフォルト ページに含めている js ファイルにあり、それをサンキュー ページで使用して Web サービスを呼び出して支払いトランザクションを追跡します。

// 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);
          }
    });
});}

firebug コンソールでは、data.d が null であることを示しています

親切に私を助けて、私が間違っているところを指摘してください。ありがとう

4

1 に答える 1

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);
于 2012-07-29T11:00:35.243 に答える