简体   繁体   中英

script not showing any alert

here i am supposed to call a web service in php and the return json of the web service is stored in a variable called $response,then i am passing that json to javascript ,here i am parsing the json and depending on the type of the employee and each type have differebt attributes i am alerting all ,when i have did the same function in another page for testing it was working where i have given value to var txt='' by hardcoding , when i have integrated the php web service with the one i havew tried nothing is having ,i am confused there is no error showing with javascript console.

 <?php
session_start();
$regid=$_SESSION['product_registration_id'];
//echo $regid;
$details=array(
'product_registration_id'=> "$regid");
//coverting the vlaues collected from form into json
//calling the web service
$url='webservice url';
    $data=$details;
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($details));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    $response= curl_exec($ch);
    echo ("The Server Response is:" .$response);
    curl_close($ch);
    json_decode($response);
    $json_a=json_decode($response,true);
    echo $json_a[expired];
    echo $json_a[account_detail][0];
 ?>
</div>
    <script>

var txt = '<?php echo $response ?>';
alert(txt);
//var jsonData = eval ("(" + txt + ")");
var jsonData = JSON.parse(txt);
for (var i = 0; i < jsonData.employees.length; i++) {
var counter = jsonData.employees[i];
//console.log(counter.counter_name);
alert(counter.type);
if(counter.type=="0")
    {
      alert(counter.building_name);
      alert(counter.org_name);
      alert(counter.user_name);
      alert(counter.name);
      alert(counter.loc_name);
      alert(counter.email_id);
      alert(counter.password);
    }
if(counter.type=="1")
    {
        alert(counter.user_name);
        alert(counter.name);
        alert(counter.password);
        alert(counter.email_id);
    }
    if(counter.type=="2")
        {
            alert(counter.building_name);
            alert(counter.org_name);
            alert(counter.user_name);
            alert(counter.opr_code);
            alert(counter.name);
            alert(counter.loc_name);
            alert(counter.email_id);
            alert(counter.password);
        }
        if(counter.type=="3")
            {
               alert(counter.building_name);
               alert(counter.org_name);
               alert(counter.machine_type);
               alert(counter.activate_status);
               alert(counter.machine_name);
               alert(counter.entrance_exit_name);
               alert(counter.entrance_or_exit);
               alert(counter.loc_name);
               alert(counter.activation_code);
            }
}

</script>

if you want the php array to be an array in javascript you must:

<?php echo json_encode($response) ?>

this does not need to be parsed in javascript, it will already be an array because the echo will return something in the likings of {'message': 'hellew world'} of ['value1','value2'] which in javascript is an array or an object definition.

So remove the parsing in javascript.

If response contains a quote you will get a js syntax error. Nothing from there on will be processed. So... no alerts. Check the response. Escape the quotes.

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