提问人:murilo frederico 提问时间:1/28/2022 最后编辑:murilo frederico 更新时间:1/29/2022 访问量:105
我想使用客户端服务器 onclick 事件创建数据扩展,我做错了什么?
I want to create a data extension using client server onclick event, what am I doing wrong?
问:
我希望在单击按钮时创建一个数据扩展,但我的代码在 SSJS 中,我无法在 onclick 事件中使用它,所以我发现 AJAX 可以帮助我,但它仍然不起作用
我一直在尝试什么 ->
<script>
function ajax() {
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest;
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
alert("status " + xmlHttp.status); alert("status " + xmlHttp.readyState)
console.log(xmlHttp.responseText)};
xmlHttp.open('POST','https://...', true);
xmlHttp.send();
}
</script> <script>
function create() {
var DE = "MyDE"
var CustomerKey = "CK123"
try {
var obj = {
"CustomerKey" :CustomerKey,
"Name" : DE,
"Fields" : [
{ "Name" : "Id", "FieldType" : "Number", "IsPrimaryKey" : true, "IsRequired" : true },
{ "Name" : "MyData", "FieldType" : "Text", "MaxLength" : 50 },
{ "Name" : "Active", "FieldType" : "Boolean", "DefaultValue" : true }
]
};
DataExtension.Add(obj);
} catch (err){
alert("(!) Data Extension was not created. Error message: " + err + "<br>")}
}
</script>
<button onclick="create();ajax()">
Request
</button>
答:
0赞
Alex Kapustin
1/28/2022
#1
首先,正确格式化你的代码,你会看到你所有的问题:)
您的 console.log(xmlHttp.responseText) 不在 onreadystatechange 函数中
xmlHttp.onreadystatechange = 函数() { if(xmlHttp.readyState == 4 && xmlHttp.status == 200) 警报(“状态” + xmlHttp.status);警报(“状态” + xmlHttp.readyState)}
这是处理程序中的内容。
另外,请注意,只有在以下情况下,您才会有 alert(“status ” + xmlHttp.status): xmlHttp.readyState == 4 && xmlHttp.status == 200
而 alert(“status ” + xmlHttp.readyState) 将一直被触发。
结论:格式化你的代码!
评论