如何使用javascript+;为多个域设置cookie;已经通过Bjron Holines商店定位器插件在系统上提供了现有的ColdFusion代码
为了展示一个商店定位器,我正在使用Bjorn Holines商店定位器插件。这个插件有一个html模板,它在所有商店的地图之外创建一个包含详细信息的列表。我在这个模板中添加了一个按钮。目标是在单击此按钮时存储具有特定分支id的cookie。应在多个域(无子域)上设置此cookie。我创建了一个函数来调用cfc。此cfc调用已存在的自定义标记,其中Cookie通过使用img scr=“…”进行设置。 请注意,我对ajax/json没有任何经验或知识。我看到很多障碍;returnformat、returntypes、cfoutputs、img、cfsavecontent、调用自定义标记的返回值等。使用这段代码,我得到了一个succes TRUE,结果+javascript警报,但显然没有存储cookie。我如何使用这个已经存在的自定义标记来设置cookies?有可能吗 hmtl模板中的按钮+功能:如何使用javascript+;为多个域设置cookie;已经通过Bjron Holines商店定位器插件在系统上提供了现有的ColdFusion代码,javascript,json,ajax,coldfusion,Javascript,Json,Ajax,Coldfusion,为了展示一个商店定位器,我正在使用Bjorn Holines商店定位器插件。这个插件有一个html模板,它在所有商店的地图之外创建一个包含详细信息的列表。我在这个模板中添加了一个按钮。目标是在单击此按钮时存储具有特定分支id的cookie。应在多个域(无子域)上设置此cookie。我创建了一个函数来调用cfc。此cfc调用已存在的自定义标记,其中Cookie通过使用img scr=“…”进行设置。 请注意,我对ajax/json没有任何经验或知识。我看到很多障碍;returnformat、ret
<button type="button" class="btn btn-sm" id="voorkeur" onClick="setCookie('{{branch}}','{{name}}')">Als voorkeur instellen</button>
<script>
function setCookie(branch, name) {
$.ajax({
url: "siteparts/branch/setCookieMultipleDomains.cfc"
, type: "POST"
, dataType: "json"
, data: {"method" : "setCookie", "returnFormat": "json", "branch": branch}
}).done(function(response) {
console.log("response", response);
alert('Uw voorkeur voor ' + name + ' is opgeslagen.');
}).fail(function(jqXHR, textStatus, errorMessage) {
console.log("errorMessage",errorMessage);
});
}
</script>
{{#location}}
<li data-markerid="{{markerid}}">
<div class="list-label"><img src="{{marker}}" /></div>
<div class="list-details">
<div class="list-content">
<div class="loc-name">{{name}}</div>
<div class="loc-addr">{{address}}</div>
{{#if address2}}
<div class="loc-addr2">{{address2}}</div>
{{/if}}
<div class="loc-addr3">{{postal}} {{city}}</div>
{{#if web}}
<div class="loc-web"><a href="http://{{web}}" target="_blank">{{niceURL web}}</a></div>
{{/if}}
{{#if distance}}
<div class="loc-dist loc-default-dist">{{distance}} {{length}}</div>
{{#if altdistance}}<div class="loc-dist loc-alt-dist">{{altdistance}} {{altlength}}</div>
{{/if}}
{{/if}}
<div class="loc-directions"><a href="https://maps.google.com/maps?saddr={{origin}}&daddr={{address}} {{address2}} {{city}}, {{state}} {{postal}}" target="_blank">Route</a></div>
<div id="cImgHolder"></div>
<button type="button" class="btn btn-sm" id="voorkeur" onClick="setCookie('{{branch}}','{{name}}')">Als voorkeur instellen</button>
<script>
function setCookie(branch, name) {
$.ajax({
url: "decokay2015/siteparts/branch/setCookieMultipleDomains.cfc"
, type: "POST"
, dataType: "json"
, data: {"method" : "setCookie", "returnFormat": "json", "branch": branch}
}).done(function(response) {
console.log("response", response);
var cArr = response.split(',');
console.log("cArr", cArr);
for (var i = 0; i < cArr.length; i++) {
var elem = document.createElement("img");
elem.setAttribute("src", cArr[i]);
elem.setAttribute("height", "1");
elem.setAttribute("width", "1");
document.getElementById("cImgHolder").appendChild(elem);
}
alert('Uw voorkeur voor ' + name + ' is opgeslagen.');
}).fail(function(jqXHR, textStatus, errorMessage) {
console.log("errorMessage",errorMessage);
});
}
</script>
</div>
</div>
</li>
{{/location}}
Als voorkerinstellen
函数setCookie(分支、名称){
$.ajax({
url:“siteparts/branch/setCookieMultipleDomains.cfc”
,类型:“POST”
,数据类型:“json”
,数据:{“方法”:“setCookie”,“returnFormat”:“json”,“branch”:branch}
}).完成(功能(响应){
控制台日志(“响应”,响应);
警报('Uw voorkeur voor'+名称+'为opgeslagen');
}).fail(函数(jqXHR、textStatus、errorMessage){
日志(“errorMessage”,errorMessage);
});
}
cfc设置CookieMultipleDomains.cfc:
<cfcomponent>
<cffunction name="setCookie" returntype="struct" access="remote">
<cfargument name="branch" type="string" required="true">
<cfset lresponse = {success=true}>
<cftry>
<CF_CU_cookieding action="setCookies" branch="#branch#" ret="qGetCookies"> --->
<cfcatch>
<cfset lresponse = {success=false}>
</cfcatch>
</cftry>
<cfreturn lresponse>
</cffunction>
</cfcomponent>
--->
自定义标记CF_CU_cookieding.cfm:
<cfsetting enablecfoutputonly="yes">
<cfparam name="attributes.action" default="">
<cfparam name="attributes.branch" default="">
<cfparam name="attributes.ret" default="">
<cfif attributes.action eq "setCookies">
<cfsavecontent variable="thecontent">
<cfoutput><img src="https://www.domain1.com/scripts/ajax/save/setCookie.cfm<cfif attributes.branch neq "">?branch=#attributes.branch#</cfif>" width="1" height="1"></cfoutput>
<cfoutput><img src="https://www.domain2.com/scripts/ajax/save/setCookie.cfm<cfif attributes.branch neq "">?branch=#attributes.branch#</cfif>" width="1" height="1"></cfoutput>
<cfoutput><img src="https://www.domain3.com/scripts/ajax/save/setCookie.cfm<cfif attributes.branch neq "">?branch=#attributes.branch#</cfif>" width="1" height="1"></cfoutput>
<cfoutput><img src="https://www.domain4.com/scripts/ajax/save/setCookie.cfm<cfif attributes.branch neq "">?branch=#attributes.branch#</cfif>" width="1" height="1"></cfoutput>
</cfsavecontent>
<cfif attributes.ret neq "">
<cfset setvariable("caller.#attributes.ret#", thecontent)>
<cfelse>
<cfoutput>#thecontent#</cfoutput>
</cfif>
<cfelseif attributes.action eq "createCookie">
<cfcookie name="d_cookie_ok" value="1" expires="never">
<cfif attributes.branch neq "">
<cfcookie name="d_cookie_branch" value="#attributes.branch#" expires="never">
</cfif>
</cfif>
?分支=#属性。分支#“width=“1”height=“1”>
?分支=#属性。分支#“width=“1”height=“1”>
?分支=#属性。分支#“width=“1”height=“1”>
?分支=#属性。分支#“width=“1”height=“1”>
#内容#
和img scr setCookie.cfm:
<cfparam name="request.branch" default="">
<cfif request.branch neq "">
<CF_CU_cookieding action="createCookie" branch="#request.branch#">
<cfelse>
<CF_CU_cookieding action="createCookie">
</cfif>
您不能在不同的域中设置Cookie,如果您有DNS访问权限,您可以使用子域设置来解决此问题 example.com
此.example.com通过使用自定义标记cu_cookieding的返回值qGetCookies修复。在我的cfc中,我将returntype更改为“any”。 我将QGetCookie从不必要的文本中剥离出来,使其仅具有指向img的路径,以逗号分隔。将响应发回。 在html模板中的javascript中,我为未来的img添加了一个div。我根据cfc响应创建了一个数组。 在数组中循环以创建图像。现在为多个域设置cookie cfc:
<cfcomponent>
<cffunction name="setCookie" returntype="any" access="remote">
<cfargument name="branch" type="string" required="true">
<cftry>
<CF_CU_cookieding action="setCookies" branch="#branch#" ret="qGetCookies">
<cfcatch>
<cfset lresponse = {success=false}>
</cfcatch>
</cftry>
<cfset lresponse = replace(qGetCookies,'><','>,<','all')>
<cfset lresponse = replace(lresponse, '<img src="','','all')>
<cfset lresponse = replace(lresponse,'" width="1" height="1">','','all')>
<cfreturn lresponse>
</cffunction>
</cfcomponent>
您可以在不同的域中设置cookie。上面的ColdFusion自定义标记为不同的域设置cookies,这是我们系统上的一个工作示例。这个customt标记是从ColdFusion中调用的。现在我想从html模板中的javascript调用它。