duangan6133 2017-03-11 17:08
浏览 68
已采纳

如何在Javascript中阅读Web服务响应的元素

I'm new in Javascript, PHP and XML. My question goes like this. I'm using this web service http://www.webservicex.net/country.asmx?op=GetCountries (This is the WSDL: http://www.webservicex.net/country.asmx?WSDL) I'm using Jquery BTW. And as part of my app, I'm trying to get every country from the response into an HTML select via Javascript. But I can't find how to get access to every specific element of that response (countries).

These are my pieces of code so far:

PHP

<?php
    $url1 = "http://www.webservicex.net/country.asmx?WSDL";
    $client = new SoapClient($url1);
    $Res = $client->GetCountries();
    echo json_encode($Res);
?>

Javascript

Here I should populate the select when load, but I can't even get access to an element of the response so I'm just trying to print something in a div element for testing purposes.

  window.onload = function() {
                      $.post("process.php", {}, function(response) {
                            var str_res = JSON.parse(response);
                            document.getElementById("Section2").innerHTML = str_res[1];
                            console.log(response);
                       }
      }

I know that "str_res[1]" won't work, but that's exactly what I'm trying to figure out. How do I access the response elements. For example get "Aruba" and print it or add it to the select I mentioned. I hope I was able to explain my concern, and thanks in advance!

  • 写回答

1条回答 默认 最新

  • douzhuanqian8244 2017-03-11 18:40
    关注

    Try this

    test.html file

    <html>
        <body>
            this is html page, wait to load result
    
            <select id="dropdown"></select>
    
            <script
                          src="https://code.jquery.com/jquery-3.1.1.min.js"
                          integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
                          crossorigin="anonymous"></script>
            <script type="text/javascript">
            $.post("test.php", {}, function(response) {
                    var str_res = JSON.parse(response); 
                   //   console.log(str_res);
                    $.each(str_res, function(i, item) {
                        //console.log(str_res[i]);
                        //console.log('-------');   
    
                        $('#dropdown').append($('<option>', { 
                            value: str_res[i],
                            text : str_res[i] 
                        }));
                    });
    
    
                });
            </script>
    
    
        </body>
    </html>
    

    test.php file

    <?php
        $url1 = "http://www.webservicex.net/country.asmx?WSDL";
        $client = new SoapClient($url1);
        $Res = $client->GetCountries();
    
        $xml = simplexml_load_string($Res->GetCountriesResult);
        $data = array();
        foreach ($xml->Table as $key => $value) {
            array_push($data,(string)$value->Name);
        }
    
        //print_r($data);
        echo json_encode($data);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用