2015-04-04 20 views
1

請原諒我的第一篇文章!我能夠成功地發送「硬編碼」 global_merge_vars與mandrill合併標籤不接受變量值

`"autotext": "true", 
    "merge": "true", 
    "global_merge_vars": [ 
     { 
      "vars": 
       { 
        "name": "LSMESSAGE", 
        "content": "hardcoded" 
       } 

     } 
    ], 

    "subject": "*|LSMESSAGE|*", 
    "html": "<p> *|LSMESSAGE|* </p>"` 

但我無法名爲ctlsm變量傳遞到「內容」。

"autotext": "true", 
    "merge": "true", 
    "global_merge_vars": [ 
     { 
      "vars": 
       { 
        "name": "LSMESSAGE", 
        "content": ctlsm 
       } 

     } 
    ], 

    "subject": "*|LSMESSAGE|*", 
    "html": "<p> *|LSMESSAGE|* </p>" 

我只是在我的郵件中得到star * | LSMESSAGE | * star。 第一個問題 - 是否可以在這裏使用一個變量? (我嘗試把ctlsm放在引號中:「ctlsm」) 如果是,我在做什麼錯?該變量是在mandrill帖子之前聲明的,並且有一個值,但似乎在$ ajax部分中丟失了它的值。

// send email using mandrill and API key 
     $.ajax({ 
type: "POST", 
url: "https://mandrillapp.com/api/1.0/messages/send.json", 
data: { etc etc 

回答

0

加+號CONCAT字符串中的JavaScript

"content": "+ctlsm+" 

這裏是用字符串變量CONCAT例子。

var text = "hello" 
document.write(text+" world"); /// this will write hello world 
+0

這將是很好,如果你可以形容,*爲什麼*這解決了問題。 (VLQRQ) – 2015-04-04 10:15:24

+0

這很簡單。如果你想在JavaScript中使用任何字符串的變量,然後使用+運算符,你可以在字符串內連接變量 – 2015-04-04 10:17:54

0

感謝Nishit。它現在有效。從代碼中刪除了「var」,將「subject」和「html」代碼移到頂端,並在mandril站點上的代碼片段中添加了「merge_language」:「mailchimp」。變量名稱ctlsm沒有引號。無論如何,這是訣竅。下面是工作代碼:

// send email using mandrill and API key$.ajax({ 
type: "POST", 
url: "https://mandrillapp.com/api/1.0/messages/send.json", 
data: { 
"key": "yourapikeyhere", 
"message": { 
"subject": "*|LSMESSAGE|*", 
"html": "<p> *|LSMESSAGE|* </p>", 
"text": " *|LSMESSAGE|*", 
"from_email": "youremailhere", 
    "to": [ 
     { 
     "email": "youremailhere", 
     "name": "yournamehere", 
     "type": "to" 
     }, 
     { 
     "email": "youremailhere", 
     "name": "yournamehere", 
     "type": "to" 
     } 
    ], 

    "autotext": "true", 
    "merge": "true", 
    "merge_language": "mailchimp", 
    "global_merge_vars": [ 
     { 
      "name": "LSMESSAGE", 
      "content": ctlsm 

     } 
    ] 



} 

}