Ceyhun Tekin
Ceyhun Tekin

Reputation: 39

how to use Meteor.http.call

I am trying to send sms using Meteor.http.call.I take two errors:

First error:When page loaded,"WebSocket connection to 'ws://localhost:3000/sockjs/632/i0uapg48/websocket' failed: WebSocket is closed before the connection is established."

Second error:when I click ebultenkydet,"Uncaught TypeError: Cannot read property 'call' of undefined"

Template.footerLayout.events({
'click #ebultenkaydet': function(e, template) {
var auth_url="http://api.sorentesms.com/index.php"
var result = Meteor.http.call("POST", auth_url, {
data: {
              'apiNo':'1',
              'user':'test',
              'pass':'test123',

              'message':'hi',
              'number':'+905075587***',
              'from':'test',

           },
           headers: {
                 "content-type":"application/json",
                 "Accept":"application/json"
           },
         })
         }
 });

Can you help me about it? Thank you all

Upvotes: 0

Views: 1740

Answers (1)

jwall
jwall

Reputation: 759

You are sending your http request inside a client side block, and Meteor.http is only available on sever side. You have to put this block in a Meteor.isServer block.

Don't forget to meteor add http to able to use the code:

Let me rewrite your code:

if (Meteor.isServer) {
  Meteor.methods({
    authCall: function () {
      this.unblock(); // Make sure server doesn't get block from this call
      var auth_url="http://api.sorentesms.com/index.php";
      return Meteor.http.call("POST", auth_url, {
        data: {
          'apiNo':'1',
          'user':'test',
          'pass':'test123',
          'message':'hi',
          'number':'+905075587***',
          'from':'test',
        },
        headers: {
          "content-type":"application/json",
          "Accept":"application/json"
        },
      })
    }
  });
}


Template.footerLayout.events({
'click #ebultenkaydet': function(e, template) {
    Meteor.call("authCall", function(error, results) {
        console.log(results); //results.data should be a JSON object
    });
});

Upvotes: 1

Related Questions