繁体   English   中英

获取 ajax 请求的响应文本

[英]get response text of ajax request

您好,我正在使用消息 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我得到这个:

图片

在控制台中我得到了这个:

图片

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

正如我在上面评论的那样,我永远不会仅通过使用 JavaScript 来执行此操作,因为它需要我将远程 API 的登录凭据发送给客户端。

I'd do it by using a PHP script, that does the request to the remote API and outputs the result that I can read out with a javascript AJAX request.

在服务器上,需要启用“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 的 output 初始化成功。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM