[英]Ajax parse error. Ajax returning error after success. Php working fine but the response is still in error
Ajax 解析錯誤。 Ajax 成功后返回錯誤。 PHP 工作正常,但響應仍然出錯。 該代碼適用於 curl 但不適用於 xml。 I'm not a pro in xml but the API return xml response so I am forced to use xml I can't identify the error SMS is being sent and database is successfully updated but the ajax is not properly working. 希望我已經解釋得夠多了
這是發送表單數據的 ajax 代碼
$.ajax({
data:$('form#candidateForm').serialize(),
type:"POST",
url:"php-modules/registeration.php",
success:function(msg){
alert(typeof msg);
if(typeof msg =='object'){
$("#otpc_id").val(msg.c_id);
jQuery('.careerfy-modal').removeClass('fade-in').addClass('fade'); jQuery('body').removeClass('careerfy-modal-active'); jobsearch_modal_popup_open("otpVerification");
}
else{
alert(msg);
}
},
error: function(request, error){
console.log(arguments);
alert(" Can't do because: " + error);
alert("Error connecting to file");
}
});
php 向數據庫中插入數據成功后發送otp消息並返回xml響應的代碼
//OTP SMS Variables
$message='Your OTP Code '.$rand_no.'.';
// OTP FUNCTION
$sessionKey=getSessionId();
$ph_number=$GLOBALS['c_phone'];
$phone = preg_replace('/[^\dxX]/', '', $ph_number);
$send = sendSmsMessage($message,$phone,'OEC-GoP',$sessionKey );
//echo "1";
//IF SUCCESSFULLY MESSAGE IS SENT
if($send){
echo $send;
$candidate_id = array("c_id"=>"$last_id");
header("Content-Type: application/json");
echo json_encode($candidate_id);
}else{
echo "measageNotSent";
}
xml代碼
function sendSmsMessage($messageText,$toNumbersCsv,$mask,$sessionKey)
{
global $planetbeyondApiSendSmsUrl;
$sessionKey=getSessionId();
$url=str_replace("#message_text#",urlencode($messageText),$planetbeyondApiSendSmsUrl);
$url=str_replace("#to_number_csv#",$toNumbersCsv,$url);
$url=str_replace("#from_number#",$fromNumber,$url);
$urlWithSessionKey=str_replace("#session_id#",$sessionKey,$url);
if($mask!=null)
{
$urlWithSessionKey = $urlWithSessionKey . "&mask=" . $mask;
}
$xml=sendApiCall($urlWithSessionKey);
return $xml->data;
}
/**
Sends Http request to api
*/
function sendApiCall($url)
{
$response = file_get_contents($url);
$xml=simplexml_load_string($response) or die("Error: Cannot create object");
if($xml && !empty($xml->response))
{
return $xml;
}
return "";
}
function getSessionId()
{
global $userName,$password,$planetbeyondApiUrl;
$url=str_replace("#username#",$userName,$planetbeyondApiUrl);
$url=str_replace("#password#",$password,$url);
$response = sendApiCall ($url);
if($response && substr($response->response,0,5)!=="Error")
{
return $response->data;
}
return -1;
}
由於未設置 fromNumber 變量而發生錯誤。 因此,php 使用 Json 代碼附加了錯誤,刪除了額外的代碼行,代碼運行正常。 // $url=str_replace("#from_number#",$fromNumber,$url);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.