meteor http发布到其他域名

时间:2014-08-07 06:22:09

标签: javascript http post meteor

所以我想在我的流星项目中使用46elks的短信服务。以下php脚本允许您发送短信:

<?
// Example to send SMS using the 46elks service
// Change $username, $password and the mobile number to send to

function sendSMS ($sms) {

  // Set your 46elks API username and API password here
  // You can find them at https://dashboard.46elks.com/
  $username = 'u2c11ef65b429a8e16ccb1f960d02c734';
  $password = 'C0ACCEEC0FAFE879189DD5D57F6EC348';

  $context = stream_context_create(array(
    'http' => array(
      'method' => 'POST',
      'header'  => "Authorization: Basic ".
                   base64_encode($username.':'.$password). "\r\n".
                   "Content-type: application/x-www-form-urlencoded\r\n",
      'content' => http_build_query($sms),
      'timeout' => 10
  )));

  return false !== file_get_contents(
    'https://api.46elks.com/a1/SMS', false, $context );
}


$sms = array(
  'from' => 'DummyFrom',   /* Can be up to 11 alphanumeric characters */
  'to' => '+46400000000',  /* The mobile number you want to send to */
  'message' => 'Hello hello!'
);
sendSMS ($sms);

?>

现在我需要在我的流星项目中使用它,并且我一直在尝试将其转换为流星http.call():

HTTP.call("POST", "https://api.46elks.com/a1/SMS", {
                headers:
                {
                    "Authorization": "Basic SomeLongBase46EncodedString",
                    "Content-type": "application/x-www-form-urlencoded"
                },

                data:
                {
                    "from": "testFrom",
                    "to": "+46701111111",
                    "message": "test message"
                }
            },

            function (error, result)
            {
                if (error)
                {
                    console.log("error: " + error);
                }
                else
                {
                    console.log("result: " + result);
                }
            });

但我一直得到的是以下错误:

error: Error: failed [403] Missing key from

2 个答案:

答案 0 :(得分:4)

data更改为params

params: {
      "from": "testFrom",
      "to": "+46701111111",
      "message": "test message"
}

并使用auth代替Authorizationdocs):

"auth": username + ":" + password

答案 1 :(得分:2)

好的,我不得不用以下格式化的字符串替换data: { ... }

content: "from=testFrom&to=+46701111111&message=test message"

这可以正确转换php http_build_query()。