I'm trying to use an API to send emails through MailChimp. I can avoid using any sort of back end by doing this. I don't know if I am setting up my Jquery script file wrong.
$(document.ready(function () {
$('#survey').click(function() {
$.ajax({
type: “POST”,
url: “https://mandrillapp.com/api/1.0/messages/send.json”,
data: {
‘key’: ‘api Key’,
‘message’: {
‘from_email’: ‘email’,
‘to’: [
{
‘email’: ‘email’,
‘name’: ‘RECIPIENT NAME (OPTIONAL)’,
‘type’: ‘to’
}
],
‘autotext’: ‘true’,
‘subject’: ‘Class Survey’,
‘html’: ‘Here is your survey link: <a>link</a>’
}
}
}).done(function(response) {
console.log(response);
});
});
});
Here are all the errors I am receiving in VS Code
I am not sure why VS Code is highlighting all of the code. I also wanted to mention the console is giving this error even though it doesnt give much info.
Uncaught SyntaxError: Invalid or unexpected token
Thanks for the help!
it's because of the wrong double quotes you are using
use this " instead of this “
use this ' instead of ‘
$(document).ready(function() {
$('#survey').click(function() {
$.ajax({
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data: {
'key': 'api Key',
'message': {
'from_email': 'email',
'to': [
{
'email': 'email',
'name': 'RECIPIENT NAME (OPTIONAL)',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'Class Survey',
'html': 'Here is your survey link: <a>link</a>'
}
}
}).done(function(response) {
console.log(response);
});
});
});
Related
I'm trying to send an object that looks like this
var postBody = {
id: userID,
objectID: [0, 1],
objects: [
{id: 0, x: 0.33930041152263374, y: 0.08145246913580247, width: 0.0823045267489712, height: 0.30864197530864196},
{id: 1, x: 0.5277777777777778, y: 0.08453888888888889, width: 0.0823045267489712, height: 0.30864197530864196}
]
};
this is ajax call
$.ajax({
type: 'POST',
url: url,
data: postBody,
dataType: 'JSONP',
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
}
});
this is what it looks like on server
{ id: '583f137fc338b517ec467643',
'objectID[]': [ '0', '1' ],
'objects[0][id]': '0',
'objects[0][x]': '0.33930041152263374',
'objects[0][y]': '0.08145246913580247',
'objects[0][width]': '0.0823045267489712',
'objects[0][height]': '0.30864197530864196',
'objects[1][id]': '1',
'objects[1][x]': '0.5277777777777778',
'objects[1][y]': '0.08453888888888889',
'objects[1][width]': '0.0823045267489712',
'objects[1][height]': '0.30864197530864196' }
if I put data: JSON.stringify(postBody) this is what I get
{ '{"id":"583f137fc338b517ec467643","objectID":[0,1],"objects":[{"id":0,"x":0.5,"y":0.5,"width":0.1,"height":0.1},{"id":1,"x":0.5401234567901234,"y":0.1833043209876543,"width":0.0823045267489712,"height":0.30864197530864196}]}': '' }
And it works! but then I cannot JSON.parse() it
this is what I get when I try
TypeError: Cannot convert object to primitive value
and all that Im doing on data on backend is this
console.log(JSON.parse(req.body)); // in the first example it was the same thing but only without JSON.parse()
Anyone have an idea why this is happening here?
Even if you have a suggestion on what I could try feel free to post here, otherwise I'm gonna have to write a function for parsing these badass inputs lol.
All the parsing/stringifying is handled by Node, so don't worry about it, just pass your object as it is, and you'll be able to access its properties in req.body.
client :
$.ajax({
type: 'POST',
url: url,
data: postBody,
dataType: 'json',
success: function(data) {
console.log(data);
},
error: function(err) {
console.log(err);
}
});
server :
console.log(req.body); //should give you [Object object]
you can then send it right back with: res.status(200).json(req.body));
and read it in your ajax callback :
success: function(data) {
console.log(data);
},
We have a scheduling system on our site.
We're trying to set it up so when someone clicks the button, along with the command the button already has, it will send an email through Mandrill.
Currently the button is under the class, .bookingjs-form-button.
Our question is, how can we specify the following java/jQuery to work with just the .bookingjs-form-button. The system is embedded, we have access to everything but the HTML, which is why we can't simply do the following.
Thanks in advance!
HTML
<form name="mail">
<input type="button" value="Click!" onClick="sendMail()">
</form>
JavaScript
function sendMail() {
$.ajax({
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data: {
'key': 'YOUR_KEY',
'message': {
'from_email': 'YOUR_SENDER#example.com',
'to': [{
'email': 'YOUR_RECEIVER#example.com',
'name': 'YOUR_RECEIVER_NAME',
'type': 'to'
}],
'subject': 'title',
'html': 'html can be used'
}
}
});
}
Your code is working, but you can use it like so:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<div id='send'>Send</div>
<script>
$('#send').click(function() {
$.ajax({
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data: {
'key': 'YOUR_KEY',
'message': {
'from_email': 'YOUR_SENDER#example.com',
'to': [{
'email': 'YOUR_RECEIVER#example.com',
'name': 'YOUR_RECEIVER_NAME',
'type': 'to'
}],
'subject': 'title',
'html': 'html can be used'
}
}
});
});
</script>
I'm trying to run this generated by facebook code on my site, but it is not working. I'm getting error "SyntaxError: missing } after property list".
Code:
FB.api(
'me/objects/my-app-name-was-here:photo',
'post',
{
og:url: http://samples.ogp.me/MYAPPIDWASHERE,
og:title: Sample Photo,
og:type: my-app-name-was-here:photo,
og:image: https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png,
og:description: ,
fb:app_id: MYAPPIDWASHERE
},
function(response) {
// handle the response
}
);
What is wrong with this code? I got it from here:
https://developers.facebook.com/apps/MY_APP_ID_WAS_HERE/open-graph/object-types/
the API you are trying to use here is
FB.api('/me/feed', 'post', { message: body }, function(response) {})
In your code, the object part(the third argument), is not a valid object, and that's why the error. please provide a valid object here.
And by the way, the link you provided is broken
Thanks
FB.api(
'me/objects/my-app-name-was-here:photo',
'post',
{
url: 'http://samples.ogp.me/MYAPPIDWASHERE',
title: 'Sample Photo',
type: 'my-app-name-was-here:photo',
image: 'https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png',
description:'' ,
app_id: 'MYAPPIDWASHERE'
},
function(response) {
// handle the response
}
);
Facebook api third param should be an object. like {fields: 'value'}
For a locally hosted HTML page, I need to email a bit of user input (name, email, comments).
This is done on a touchscreen display, where visitors can leave their information and comments.
So I was thinking of using a virtual keyboard such as here.
<form action="post" id="emails" onsubmit="return false;">
<input type="text" id="keyboard" placeholder="Enter name..."></input>
<input type="text" id="keyboard2" placeholder="Enter email..."></input>
<input type='submit' id='send' value='Submit' />
</form>
I used this to be able to send the email link.
And with the correct details, the following code works and I get emails in my inbox.
$('#send').click(function() {
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': "xxxxxxxx",
'message': {
'from_email': "xxxxxx#xxxxxx.com",
'to': [
{
'email': "xxxxxxxx#xxxxxxxxxx.com",
'name': 'xxxxxx',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'TEST! TEST!',
'html': 'test'
}
}
}).done(function(response) {
console.log(response);
alert("You send an email!"); // if you're into that sorta thing
});
});
So this works, as in it sends the email with the text put in here: 'html': 'test'
And instead of 'test', I would obviously like to get 'name + email'.
(Also it is probably for less then 50 emails per month, so just an email will suit my needs for now, nothing with databases and saving all this data. I would just like to get the email.)
So, is it possible and how should I do this?
Is the route I took now doable or would you suggest a completely different way?
// do everything inside after page load.
$(function() {
$('#send').click(function() {
// get values of inputs...
var name = $('#keyboard').val(),
email = $('#keyboard2').val();
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': "xxxxxxxx",
'message': {
'from_email': "xxxxxx#xxxxxx.com",
'to': [
{
'email': "xxxxxxxx#xxxxxxxxxx.com",
'name': 'xxxxxx',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'TEST! TEST!',
'html': 'Name: ' + name + '\nEmail: ' + email // and use it!
}
}
}).done(function(response) {
console.log(response);
alert("You send an email!"); // if you're into that sorta thing
});
});
});
I've got it working now with the following code:
$(function() {
$('#send').click(function() {
var name = $('#keyboard').val();
var email = $('#keyboard2').val();
$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': "xxxxxxxx",
'message': {
'from_email': "xxxxxx#xxxxxx.com",
'to': [
{
'email': "xxxxxxxx#xxxxxxxxxx.com",
'name': 'xxxxxx',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'TEST! TEST!',
'html': 'Name: ' + name + '\nEmail: ' + email // and use it!
}
}
}).done(function(response) {
console.log(response);
alert("You send an email!"); // if you're into that sorta thing
});
});
});
On other simular problems were value were not shown I read something about the .val() was taking the values on page load (makes sense now), so I moved them down into the click function and it gives me the result I was after.
I am trying to load a website into a div (div.content).
The website requires a login via a POST.
The javascript tries to load the site, detects the redirect to the login form, POSTs the username and password and finally reloads the url since authentication was succesful.
Now the problem is that the url is not constructed correctly. The url should be like this :
https://mysite.com/WebUntis/index.do?school=schoolname#Timetable?
onlyTimetable=true&ajaxCommand=renderTimetable&date=20130418&type=1&id=17
&formatId=2&departmentId=-1&buildingId=-1
but the jquery builds it like this:
https://mysite.com/WebUntis/index.do?
onlyTimetable=true&ajaxCommand=renderTimetable&date=20130418&type=1&id=17
&formatId=2&departmentId=-1&buildingId=-1.
Can somebody help me out here? I checked the syntax by looking at a lot of examples, but I am unable to find the fix.
function openUrl()
{
jQuery.ajax({
url: 'https://mysite.com/WebUntis/index.do?school=schoolname#Timetable?',
type:'GET',
dataType: 'html',
data: {
//school: 'schoolname',
ajaxCommand: 'renderTimetable',
onlyTimetable: 'true',
date: '20130418',
type: '1',
id: '17',
formatId: '2',
departmentId: '-1',
buildingId: '-1'
},
success: function(result) {
var html = jQuery('<div>').html(result);
if(html.find("form").attr("id") == "login")//login form found
{
alert("Trying to login...");
login(); //This just POSTs a username and password
};
$('div.content').html(result);
},
});
}
Try encoding the parameter school
function openUrl()
{
jQuery.ajax({
url: 'https://mysite.com/WebUntis/index.do',
type:'GET',
dataType: 'html',
data: {
school: encodeURIComponent('schoolname#Timetable?'),
ajaxCommand: 'renderTimetable',
onlyTimetable: 'true',
date: '20130418',
type: '1',
id: '17',
formatId: '2',
departmentId: '-1',
buildingId: '-1'
},
success: function(result) {
var html = jQuery('<div>').html(result);
if(html.find("form").attr("id") == "login")//login form found
{
alert("Trying to login...");
login(); //This just POSTs a username and password
};
$('div.content').html(result);
},
});
}