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