dongpao2871 2019-05-01 17:53
浏览 139

如何从jQuery ajax调用gatewayapi?

I am using gatewayapi.com to send sms from a php script. It works great. But when I try to call the same script from jQuery ajax it will not work. Can someone please tell me how to do it? This is my php code that works.

$recipients = ['myphonenumber'];
$url = "https://gatewayapi.com/rest/mtsms";
$api_token = "mytoken";
$json = [
    'sender' => 'mysender',
    'message' => 'mymessage',
    'recipients' => [],
];
foreach ($recipients as $msisdn) {
    $json['recipients'][] = ['msisdn' => $msisdn];
}

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch,CURLOPT_USERPWD, $api_token.":");
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($json));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

This is my javascript that do not work.

<script>
function send_sms() {
    $.ajax({
    dataType: "json",           
    type: "POST",
    async: false,
    url: 'sendsms.php',
    data: {
    message: 'mymessage'
    },
    cache: false,           
    success: function(data)
        {

        }             
    });
}
</script>

I also tried to call the php script from a cronjob but that do not work.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器