简体   繁体   中英

Ajax parse error. Ajax returning error after success. Php working fine but the response is still in error

Ajax parse error. Ajax returning error after success. PHP working fine but the response is still in error. The code works fine with curl but not with 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. Hope I have explained enough

Here is the ajax code from where the form data is being sent

$.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 code that after successfullt inserts data in the database send the otp message and returns xml response

//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 code

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;
}

The Error occurred due to fromNumber Variable was not set. So the php was attaching the Error with the Json code removing that extra line of code helped and the code is working fine. // $url=str_replace("#from_number#",$fromNumber,$url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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