The following GET request works using jQuery:
$.ajax({
url: "https://yoda.p.mashape.com/yoda?sentence="+phrase,
headers: {"X-Mashape-Key": "superSecretKey", "Accept": "text/plain"},
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
})
But the adaptation that follows using Mithril will not work:
Yoda.submit = function (phrase) {
console.log(phrase);
return m.request({
method: 'GET',
url: "https://yoda.p.mashape.com/yoda?sentence=" + phrase,
headers: {"X-Mashape-Key": "superSecretKey",
"Accept": "text/plain"}
});
}
I've tried different variations after consulting the documentation at https://lhorie.github.io/mithril/mithril.request.html and searching for similar examples. I'm considering using 3rd party libraries but thought I'd try here before I go too far down the rabbit hole. The error message that I get when I try to make an AJAX request is that I'm missing the API key even though it's right there.
UPDATE:
I've since learned, in addition to the answer marked below, that Mithril automatically parses API responses as JSON. Since I'm getting back a string response from this API, I have to include in my m.request
object the following entry:
deserialize: function(value) {return value;}
Deserialize tells Mithril to return the response value as-is, not as JSON.