【发布时间】:2018-04-25 16:38:44
【问题描述】:
我们正在尝试读取 Marketo 跟踪 cookie 的值,以帮助在我们的网站上预先填写封闭资产表格。
This link首先解释了如何使用Javascript读取cookie的值(足够简单):
//Function to read value of a cookie
function readCookie(name) {
var cookiename = 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(cookiename) == 0) return c.substring(cookiename.length,c.length);
}
return null;
}
//Call readCookie function to get value of user's Marketo cookie
var value = readCookie('_mkto_trk');
然后它解释了如何获取 cookie 的值并通过 Ruby 使用 REST API 调用 Marketo:
#NOTE: The _mkto_trk cookie value includes an ampersand and needs to be URL encoded to '%26' in order to be properly accepted by the Marketo endpoint.
require 'rest_client'
require 'json'
#Build request URL
#Replace AAA-BBB-CCC with your Marketo instance
marketo_instance = "https://AAA-BBB-CCC.mktorest.com"
endpoint = "/rest/v1/leads.json"
#Replace with your access token
auth_token = "?access_token=" + "cde42eff-aca0-48cf-a1ac-576ffec65a84:ab"
#Replace with filter type and values
filter_type_and_values = "&filterType=cookies&filterValues=id:AAA-BBB-CCC%26token:_mch-marketo.com-1418418733122-51548&fields=cookies,email"
request_url = marketo_instance + endpoint + auth_token + filter_type_and_values
#Make request
response = RestClient.get request_url
#Returns Marketo API response
puts response
我们不使用 Ruby(我们使用 Sitecore CMS)。那么有没有办法获取 cookie 的值,构建 Marketo API URL,然后仅使用 Javascript 对 Marketo 进行 REST API 调用?
【问题讨论】:
-
jQuery.ajax(或其他库) -
或
axios库:github.com/axios/axios -
通过发出 AJAX 请求,您可以使用原生 JS 语法或使用许多包装库之一,包括上面提到的那些,这通常会使语法更容易。这是假设端点接受 CORS 请求。
标签: javascript marketo