[英]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.