获取ajax请求的响应文本

时间:2019-11-08 10:24:18

标签: javascript ajax

您好,我使用的是消息API,但无法获取显示在mozilla的网络块中的答案。

在我的代码下面

function sendMessage(params){
            var url = 'https://api.1s2u.io/bulksms?username='+ params.username +'&password='+ params.password +'&mt=1&fl=0&sid='+ params.sid +'&mno='+ params.mno +'&msg='+params.msg;
            $.ajax({
                type: 'get',
                url: url,
                processData : false,
                contentType: false,
                async: false,
                success: function (data) {
                    console.log(data)
                },
                complete: function (data) {
                    console.log('complete', data)
                },
                error: function (xhr, status, error) {
                    console.log('message',xhr)
                    console.log('exeptcion', status, error)

                }
            })
        }

在mozilla中,我得到了:

image

在控制台中,我得到了:

image

我只想恢复代码050。如何?

1 个答案:

答案 0 :(得分:0)

如上所述,我永远不会仅使用JavaScript来执行此操作,因为它要求我将远程API的登录凭据发送给客户端。

我会使用PHP脚本来完成此操作,该脚本会向远程API发出请求,并输出可以用javascript AJAX请求读取的结果。

在服务器上,需要启用“ url_fopen” PHP功能。 该方法提供了对API的更多控制,因为扩展所需的所有数据都可以存储在服务器端。例如,检测到到给定手机号码或来自给定手机号码的发送请求太多,或者仅允许向注册用户发送消息的自定义帐户系统,等等。

一种快速的解决方案是像我在这里所做的那样做某事:

创建一个名为:send.php的文件:

<?php

const USERNAME = "Username";
const PASSWORD = "Password";

$MNO = $_POST['mno'] ?? '';
$SID = $_POST['sid'] ?? '';
$MSG = $_POST['msg'] ?? '';

$HttpData = http_build_query(
    array(
        "username" => USERNAME,
        "password" => PASSWORD,
        "sid" => $SID,
        "mno" => $MNO,
        "msg" => $MSG
    )
);

$HttpRequest = stream_context_create(
    array(
        'http' => array(
            'method' => 'GET',
            'header' => "Accept-Language: en\r\n"
        )
    )
);

$Response = array();
$Request = file('https://api.1s2u.io/bulksms?' . $HttpData, FILE_IGNORE_NEW_LINES, $HttpRequest);
if($Request && count($Request) > 0) {   
    $Response['success'] = $Request[0]; 
} else {
    $Response['error'] = 'There was an error in the http request!';
}

header("Content-Type: application/json");
echo json_encode($Response);

?>

您的HTML必须像这样:

<!DOCTYPE HTML>
<html lang="en">
    <head>
        <title>SMS Test</title>
        <meta charset="utf-8" />
        <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
        <script>

        $(document).on('click', '#submitSms', function(e) {
            var mno = $("#mno").val();
            var sid = $("#sid").val();
            var msg = $("#msg").val();

            var error = false;
            if(mno.length < 3) { $("#mno").css("border-color", "red"); error = true; }
            if(sid.length < 3) { $("#sid").css("border-color", "red"); error = true; }
            if(msg.length < 3) { $("#msg").css("border-color", "red"); error = true; }
            if(error) return;

            $.ajax({
                url: 'send.php',
                type: 'post',
                data: 'sid=' + sid + '&mno=' + mno + '&msg=' + msg,
                dataType: 'json',
                success: function(json) {
                    if(json['error']) {
                        $("#sendSms").prepend('<p style="color:red;">' + json['error'] + '</p>');
                    } else if(json['success']) {    
                        $("#sendSms").prepend('<p style="color:green;">' + json['success'] + '</p>');
                    }                   
                },
                error: function(xhr, opt, error) {
                    alert(error + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        });

        </script>
    </head>

    <body>
        <div id="sendSms">
            <p>MNO: <input type="text" name="mno" id="mno"></p>
            <p>SID: <input type="text" name="sid" id="sid"></p>
            <p>Message:<br><textarea name="msg" id="msg"></textarea></p>
            <p><input type="submit" name="submitSms" id="submitSms" value="Send" /></p>
        </div>
    </body>
</html>

这只是一个快速的解决方案,可能需要您自己处理一些与安全性相关的方面,因为我不知道您的项目其他部分的外观。 将Sender-ID,Message和MobileNo发布到send.php,其中将包含您的登录详细信息,并将对您要使用的其他API发出请求。它输出包含键“错误”或“成功”的JSON编码数据。对远程API的请求失败后,将设置“错误”,如果请求成功,则使用该远程API的输出初始化成功。

您只需要处理JS(或send.php)中的远程API错误代码。

相关问题