将PHP webservice json响应发送给javascript

send the php webservice json response to javascript

本文关键字:javascript 响应 PHP webservice json      更新时间:2023-09-26

我想把json中的PHP web服务响应带到javascript中,这是从PHP web服务调用中获得的这是我的代码

<?php
session_start();
$url='webservice url';
    $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($useridofuser));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    $response=  curl_exec($ch);
    //echo(''n'."Server response : 'n 'n".$response);
    curl_close($ch);
    //parsing the json response from server
    $jsonde="$response";
    $_SESSION['json']=$jsonde;
 $org = array();
 $loc = array();
 $bui = array();
 $items = json_decode($response);
 foreach( $items as $each ){
 $loc[]=$each->location[0]->name;
 $bui[]=$each->location[0]->building[0];
 $org[]=$each->name;
 }
 ?> 

这里我在$org中获得组织名称,$bui包含建筑物,$ loc包含位置在代码

下面有一个表单
    <sript>
    var json = "<?php echo $response ?>";
    alert(json);
 </script>
 <form>
 <label for="orgname">Organisation Name</label>
                    <select style="width: 305px;text-align:left ;"  name="category_id" id="category_id">
                    <option value="">Select</option>
                    <?php foreach($org as $key=>$val){?>
                    <option value="<?php echo $val; ?>"><?php echo $val;?></option>
 <?php
 }
 ?>                      </select>
                    <p>
        <label name="location">Location</label>
                     <select style="width: 305px;" name="category_id1" id="category_id1" onchange="ajax(this);">
                     <option value="">Select</option>
                     </select>
                    </p>
                    <p>
        <label for="building">Building</label>
                    <select style="width: 305px" name="category_id2" id="category_id2">
                    <option value="">Select</option>
                    </select>

在组织中,我想将我从组合框中选择的值与PHP web服务返回的json数组进行比较,并返回包含该特定组织的对象,我的问题是如何将json数组存储到javascript变量或一些东西中。谢谢你

为什么不像这样用json内容设置一个javascript变量呢?

<?php 
    $response = "YOUR JSON RESPONSE";
?>
<script type="text/javascript">
    var json = "<?php echo json_encode(json_decode($response)) ?>";
</script>

确保您以正确的方式转义$response,以防止损坏您的javascript。

这有帮助吗?

<!doctype html>
<html>
<head>
<script>
function func1()
{
    try
    {
        addedVariable = addedVariable;
        alert('can only add the script once');
    }
    catch(ex)
    {
        var sc = newEl('script');
        sc.appendChild( newTxt('var addedVariable = 100;' ) );
        document.head.appendChild(sc);
        alert('script element with variable added');
    }
}
function func2()
{
    try
    {
        alert("value of 'addedVariable' = " + addedVariable);
    }
    catch(ex)
    {
        alert("hit the first btn, then try again");
    }
}
</script>
</head>
<body>
    <button id='btn1' onclick='func1();'>click Me 1st</button>
    <button id='btn2' onclick='func2();'>click me 2nd</button>
</body>
</html>