-1

Heres my email sending function:

function send() {
var key = "dJdJekCVAFIqvUJ13DEczZjgIh_4MyeIGEHz2GBYKFe";
var message_name = "defender_send_message";
var data = {};
data.value1 = document.getElementById('textBox').value;
data.value2 = localStorage.getItem("AdminsEmail");
var url = "https://maker.ifttt.com/trigger/" + message_name + "/with/key/" + key; 

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE) {
       if (xmlhttp.status == 200) {
           console.log("Message Sent");
       }
    }
}

xmlhttp.open('POST', url, true);
xmlhttp.responseType = 'json';
xmlhttp.send(new FormData(data));
}

I wanted to create an email sending function with only pure js, not jquery or anything. I get the following errors when i click send:error

(ignore the first error i fixed that already) I had a jquery function that worked (but i had to get rid of it):

var message = localStorage.getItem("Message");
console.log(message + localStorage.getItem("AdminsEmail"));
var key = "dJdJekCVAFIqvUJ13DEczZjgIh_4MyeIGEHz2GBYKFe"; // << YOUR KEY HERE
var message_name = "defender_send_message";    // << YOUR MESSAGE NAME HERE
var url = "https://maker.ifttt.com/trigger/" + message_name + "/with/key/" + key;
$.ajax({  
  url: url,
  data: {value1: message,
         value2: localStorage.getItem("AdminsEmail")},
  dataType: "jsonp",
  complete: function(jqXHR, textStatus) {
    console.log("Message Sent");
  } 
});  

why would this work and my other function not?

sparpo
  • 99
  • 2
  • 4
  • 11

1 Answers1

2

EDIT 2 : Since it seems the endpoint doesn't actually return JSON, I think your original jQuery code wasn't correct either. You need to do more research into this iftt.com platform and how to use it. From what I can tell, it's meant to be used in a mobile app, not in the browser- it would be a normal POST XHR then, and CORS doesn't apply to mobile apps. They have this page for testing the endpoint- notice that it gives you an example using curl, a command-line tool, where again CORS doesn't apply. So I think you need to rethink things, this service is not designed to be used from a browser, like you are trying to do.


EDIT: since it turns out you are actually trying to use JSONP and not a plain XHR, all you need to do is implement that without jQuery- create a script tag with the server's URL and add a URL parameter to define your callback function to handle the data. This answer should give you the solution.

In your case the code might look like this :

http://www.codeply.com/go/bp/VRCwId81Vr

function foo(data)
{
    // do stuff with JSON
  console.log(data)
}

var script = document.createElement('script');
script.src = "https://maker.ifttt.com/trigger/defender_send_message/with/key/"+
    "dJdJekCVAFIqvUJ13DEczZjgIh_4MyeIGEHz2GBYKFe?callback=foo";

document.getElementsByTagName('head')[0].appendChild(script);

Note that this doesn't work for me(but with your code, you would get Message sent printed to the console, so maybe you thought it was working?)- the response isn't JSON. Most likely the endpoint isn't actually meant to be used for JSONP?

My answer below only applies if you are trying to do a regular XHR in a browser without JSONP.


This happens because of the Cross Origin Resource Sharing policy of your browser. Your code is hosted at localhost, and it is trying to access a resource hosted at maker.ifttt.com through an XmlHttpRequest. In order to allow this to happen, the server at maker.ifttt.com would need to be configured to allow access from the localhost origin. Presumably you can not make that change as you don't control that server.

In your case, the best solution would be to make the request to maker.ifttt.com through your own server- CORS doesn't apply for server-to-server requests. Send the XmlHttpRequest to your server, take the data regarding the email from the request URL parameters, and then make the request to maker.ifttt.com using that data.

Community
  • 1
  • 1
Karl Reid
  • 2,147
  • 1
  • 10
  • 16
  • I had an jquery ajax function that did the same thing and it could send the email (i wont go into why i needed to get rid of it). If this error never appeared with the ajax function, why should it appear with this? This leads me to believe that there is something else thats wrong, perhaps with the code? – sparpo Apr 12 '17 at 21:50
  • Can't really answer that without more info- as long as you are making the request from the localhost origin, to the maker.ifttt.com site, your browser is not going to allow the request. So using jQuery instead of native XHR shouldn't matter.. – Karl Reid Apr 12 '17 at 22:17
  • i edited my original post to show you my other function – sparpo Apr 12 '17 at 22:43
  • 1
    Oh- in your first example you were using JSONP. That's very different! JSONP actually involves creating a script tag and loading data from the other server that way, but jQuery wraps that in a convenient ajax method that hides that - it's not equivalent to an XHR at all. CORS doesn't apply to JSONP because you're not making a scripted request, instead you're just loading a resource through a tag. If you want to do JSONP without jQuery, have a look at this : http://stackoverflow.com/questions/6132796/how-to-make-a-jsonp-request-from-javascript-without-jquery It should work. – Karl Reid Apr 12 '17 at 23:47
  • I looked at that question but im not sure how to use my script with those answers.`function foo(data) { // do stuff with JSON } var script = document.createElement('script'); script.src = '//example.com/path/to/jsonp?callback=foo' document.getElementsByTagName('head')[0].appendChild(script); // or document.head.appendChild(script) in modern browsers`how do i use this with my code? – sparpo Apr 13 '17 at 00:04
  • See my edit, I added an example. But it doesn't seem like the endpoint is actually returning JSON. I think you should do a bit of thinking on your overall approach- isn't ifttt meant to be used in a mobile app, not the browser? – Karl Reid Apr 13 '17 at 10:16
  • oh yeah, this is a cordova app that im building – sparpo Apr 14 '17 at 22:27
  • also when i create the script var and "append" it, what do i actually do with it after that? Is that what sends the email? – sparpo Apr 14 '17 at 22:36
  • If we're talking about JSONP, you don't do anything with the script tag- when you create it, the browser will handle downloading the script from the url, just as it would with any other script tag in the DOM. When it's complete, the function you passed as 'callback' will be called and it can handle the data from the server. – Karl Reid Apr 18 '17 at 09:54