How can i mimic an ajax call


#1

Hello,

I should get an authentication from another website to use a java applet. I have two problems, one of them is CORS, I tried to solve it via http.call(‘get’…) from server and the other one is await/async. My solutions is always return as undefined.

But couldn’t solve the problem.

I should mimic this
$.ajax("http://********sign.php?request=" + toSign).then(resolve, reject);

and try to solve like this

client.js

return function(resolve, reject) {
    await Meteor.promiseCall('setSignaturePromise', toSign).then(resolve, reject);
}

server.js
HTTP.call('get', 'http://********sign.php?request=' + toSign);

How can i solve this problem? Thanks.


#2

You’re mixing “sync-style” async/await with Promise chains. The await ... returns with the result. You can use catch in the event of an error:

try {
  const result = await Meteor.promiseCall('setSignaturePromise', toSign);
  // do something with result
} catch(error) {
  // do something about the error
}

On the server, your setSignaturePromise method will look something like:

Meteor.methods({
  setSignaturePromise(toSign) {
    try {
      return HTTP.call('get', 'http://********sign.php?request=' + toSign);
    } catch(error) {
      throw new Meteor.Error(...);
    }
  },
});