簡體   English   中英

我有一個SMSgateway JavaScript API,我想與我的網站集成,但似乎不起作用

[英]I have a SMSgateway JavaScript API which I want to integrate with my website, somehow it doesn't seems to work

    <form onsubmit="return process()" method="post">
    <table style="background-color:rgba(0,0,0,.1); border-radius:8px">
    <tr>
        <td>Name:</td>
        <td><input type="text" name="name" id="apname" placeholder="Name" required /></td></tr>
    <tr>
        <td>Age:</td>
        <td><input type="text" name="age" id="apage" placeholder="Age" required /></td></tr>
    <tr>
        <td>Phone:</td>
        <td><input type="text" name="phone" id="apphone" placeholder="For the OTP" onchange="return validation();" required /></td></tr>
    <tr>
        <td>Date:</td>
        <td><input type="date" name="date" id="apdate" required /></td></tr>
    <tr>            
        <td></td>
        <td><button class="btn btn-success" id="button" style="width:90px" type="submit">Proceed</button>
        <button class="btn btn-danger" style="width:90px" onclick="window.location.href='index.html'">Cancel</button></td></tr>
        <span id="err" style="color:#FF0000"></span>
    </table></form>
    </div>
</div>

<script>
function process(){
    var mcAuthkey = "42A1zr6NGQ6ip";
    var theName = document.getElementById('#apname').value;
    var age = document.getElementById('#apage').value;
    var phone = document.getElementById('#apphone').value;
    var date = document.getElementById('#apdate').value;
    var msg = "theNam+ aged +age+ has booked for an appointment on +date+. Contact: +phone";
    var settings = {
    "async": true,
    "crossDomain": true,
    "url": "http://api.msg91.com/api/v2/sendsms",
    "method": "POST",
    "headers": {
    "authkey": "mcAuthkey",
    "content-type": "application/json"
    },
    "processData": false,
    "data": "{ \"sender\": \"SOCKET\", \"route\": \"4\", \"country\": \"91\", \"sms\": [ { \"message\": \"msg\", \"to\": [ \"phone\" ] } ] }"
    }

    $.ajax(settings).done(function (response) {
    console.log(response);
    });
    event.preventDefault();
    return false;
}
</script>

</body>
</html>

提交表單時,我希望“流程功能”能夠正常工作。 任何想法可能出了什么問題嗎? 我也不能使用任何框架或其他服務器端語言。 我只需要使用HTML + JavaScript。 我正在用Apache Cordova構建一個Android應用。

首先,您沒有定義驗證函數,該函數在更改第三個字段時調用。

由於您使用的是本機document.getElementById,因此您傳入的元素ID不帶散列。

您不應引用mcAuthkey,因為它是一個變量。

當然,您已經將jquery庫添加到了html中。

此REST API需要傳遞正確的身份驗證密鑰。 我將無法測試。 您必須使用有效的身份驗證密鑰。

編輯

他們的API中的示例帖子調用: http : //api.msg91.com/api/v2/sendsms?authkey=YourAuthKey&mobiles=98260XXXXX,98261XXXXX&message=message&sender=senderid&route=4&country=0

他們的指示在這里

我必須更新您的某些字段以匹配它們的結構。

以下是更新的代碼段

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form onsubmit="return process()" method="post"> <table style="background-color:rgba(0,0,0,.1); border-radius:8px"> <tr> <td>Name:</td> <td><input type="text" name="name" id="apname" placeholder="Name" required /></td></tr> <tr> <td>Country code:</td> <td><input type="text" name="country" id="country" placeholder="Country code" required /></td></tr> <tr> <td>Phone:</td> <td><input type="text" name="phone" id="apphone" placeholder="For the OTP" onchange="return validation();" required /></td></tr> <tr> <td>Date:</td> <td><input type="date" name="date" id="apdate" required /></td></tr> <tr> <td></td> <td><button class="btn btn-success" id="button" style="width:90px" type="submit">Proceed</button> <button class="btn btn-danger" style="width:90px" onclick="window.location.href='index.html'">Cancel</button></td></tr> <span id="err" style="color:#FF0000"></span> </table></form> </div> </div> <script> function validation(){ return true; } function process(){ var mcAuthkey = "42A1zr6NGQ6ip"; var theName = document.getElementById('apname').value; var country = document.getElementById('country').value; var phone = document.getElementById('apphone').value; var date = document.getElementById('apdate').value; var msg = "theNam+ aged +age+ has booked for an appointment on +date+. Contact: +phone"; var settings = { "async": true, "crossDomain": true, "url": "http://api.msg91.com/api/v2/sendsms", "method": "POST", "headers": { "authkey": mcAuthkey, "content-type": "application/json" }, "processData": false, "data": { sender: "SOCKET", route: "4", country: country, flash:1, sms: [ { "message: msg, to: phone} ] } } $.ajax(settings).done(function (response) { console.log(response); }); event.preventDefault(); return false; } </script> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM