首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的代码出了什么问题(使用dojo xhrGet Ajax)

我的代码出了什么问题(使用dojo xhrGet Ajax)
EN

Stack Overflow用户
提问于 2011-09-27 07:42:57
回答 1查看 351关注 0票数 2

我尝试使用这段代码来调用php文件、访问数据库、检索值,并使用JSON对象返回它们,然后将它们编辑到一个文本框中。

Javascript端的代码:

当用户更改下拉列表中的选项时,应用程序应该调用PHP脚本从数据库获取新值,从JSON对象检索新值,并修改文本区域以显示新值。

代码语言:javascript
运行
复制
<select id="busSelect" name="busSelect">
    <option>S053-HS - P</option>
    <option>S059-HS - P</option>
    <option>S064-HS - P</option>
    <option>S069-HS - P</option>
    <option>S070-HS - P</option>
</select>

    <textarea id="memo"></textarea>




    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js" type="text/javascript"></script>
    <script type ="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>

    <script type ="text/javascript">
        <?php

        ?>
        dojo.ready(function(){
            var myOptions = {
                zoom: 12,
                center: new google.maps.LatLng(26.4615832697227,-80.07325172424316),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            var map = new google.maps.Map(dojo.byId("map_canvas"),
            myOptions);

            dojo.connect(busSelect,"onchange",function(){

                dojo.xhrGet({

                   url: "getRoute.php",
                   handleAs: "json",
                   timeout: 1000,
                   content: {
                     route: dojo.byId("busSelect").value  
                   },

                   load: function(result) {

                       var formResult = result.lat_json + " " + result.long_json + " " + result.name_json;
                       dojo.byId(memo).value = formResult;

                   }

                });

            });

PHP脚本:

应该使用从JS应用程序接收到的名称,即"bus name“,并使用该名称查找bus id。然后它应该使用这个ID访问公共汽车站(这一切都有效,我只是弄错了JSON/AJAX位)

代码语言:javascript
运行
复制
<?php

header('Content-type: application/json');

    require_once 'database.php';

    mysql_connect($server, $user, $pw);
    mysql_select_db("busapp") or die(mysql_error());

    $route = $_GET["route"];

    $result_id = mysql_query("SELECT * FROM routes WHERE name = $route");

    $result_row = mysql_fetch_array($result_id);
    $route_id = $row['id'];

    $result = mysql_query("SELECT * FROM stops_routes WHERE route_id = $route_id")
    or die(mysql_error());  




    $markers;
    $stopid = array();
    $time = array();
    $lat;
    $long;
    $name;
    $waypts = array();


    for ($x = 0; $row = mysql_fetch_array($result); $x++) {
        $stopid[$x] = $row['stop_id'];
        $time[$x] = $row['time'];
    }

    for ($x = 0; $x < sizeof($stopid); $x++) {

        $result = mysql_query("SELECT * FROM stops WHERE id = $stopid[$x]")
        or die(mysql_error());

        $row = mysql_fetch_array($result)
        or die(mysql_error()); 

        $lat[$x] = $row['lat'];
        $long[$x] = $row['long'];
        $name[$x] = $row['name'];

    }

    $size = count($stopid);

    $lat_json = json_encode($lat);
    $long_json = json_encode($long);
    $name_json = json_encode($name);

?>

运行时,我在dojo.xd.js:14上也得到了一个错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-27 07:53:03

您应该创建一个对象,然后将其编码为json_encode(),然后简单地使用正确的Content-type头回显该JSON,而不是将单个变量传递给JSON。

代码语言:javascript
运行
复制
// Start with an associative array
$arr = array("lat_json" => $lat, "long_json" => $long, "name_json" => $name);
// Cast it to an Object of stdClass
$obj = (object)$arr;

// Encode it
$json = json_encode($obj);

// And return it to the calling AJAX by just echoing out the JSON
header("Content-type: application/json");
echo $json;
exit();

在对JSON进行编码之前,您的对象现在看起来像(使用我的示例数据):

代码语言:javascript
运行
复制
stdClass Object
(
    [lat_json] => 12345
    [long_json] => 45678
    [name_json] => the name
)

// After json_encode()
{"lat":12345,"long":45678,"name":"the name"}

既然你已经在接收端设置了你的javascript,我相信它不需要修改就可以工作。要确定javascript端的JSON结构,请确保在load()函数中检查console.dir(result)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7562617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档