javascript - meteor http 发布到其他域

标签 javascript http post meteor

所以我想在我的 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);

?>

现在我的 meteor 项目需要这个,我一直在尝试将它转换为 meteor 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);
                }
            });

但我不断得到的是以下错误:

错误:错误:失败 [403] 缺少来自的 key

最佳答案

data更改为params:

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

并使用 auth 而不是 Authorization ( docs ):

"auth": username + ":" + password

关于javascript - meteor http 发布到其他域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25175492/

相关文章:

javascript - 显示 openweathermap API 错误时出错

c# - Javascript 未在 C# 代码中触发

javascript - post请求完成后如何重定向用户

python - Selenium 很慢,还是我的代码有误?

ios - Swift JSON Post 请求

.net - 在 .NET 中构建 POST 请求正文的内置方法?

javascript - 通过键将数组中的对象推送到不同的数组

javascript - 如何在点击时显示div?

php - 带有 Web 身份验证和端口的 HTTP 请求

java - Struts 2,JasperReports 插件 : How to set file name for generated report