2012-04-01 43 views
1

在萤火虫(与FirePHP插件)我注意到,有与PHP文件中没有沟通,因此没有任何反应...

这里是JavaScript:
为什么此Dojo代码不与其'PHP文件通信?

// three parameters are required by Dojo! 
function buildMenu(type, data, evt) 
{ 
    var menuDOM = document.getElementById("colorselect"); 
    var nextColor, nextItem; 

    // delete previous items in the color menu 
    menuDOM.options.length = null; 

    // split the data into an array of colors 
    var colors = data.split(', '); 

    // go through the returned array of colors 
    for(var i = 0; i < colors.length; index++) 
    { 
     nextColor = colors[i]; 
     nextItem = new Option(nextColor); 

     /* add the new item to the menu 
     ("null" for IE5, "-1" for all other browsers) */ 
     try 
     { 
      menuDOM.add(nextItem, -1); 
     } 
     catch(e) 
     { 
      menuDOM.add(nextItem, null); 
     } 
    } 
} // end of function buildMenu 

// the function that calls bind to request data 
function getColors(size) 
{ 
    dojo.io.bind({url: "shirtColors.php" + "?size=" + size, 
        load: buildMenu, 
        method: "GET", 
        mimetype: "text/plain" 
        }); 
}<br /><br /> 

这里是PHP:

<?php 

    $shirtSize = $_GET["size"]; 

    // array for available colors (for each shirt size) 
    $colors = array("large" => "black, yellow, green", 
        "medium" => "blue, purple, white, off-white, cream, bleached-white", 
        "small" => "orange, red, aqua, turqoise, aquamarine, light-blue"); 

    echo $colors[$shirtSize]; 
?><br /><br /> 

...这里是HTML:
(道场链接到在线在这里,和Dojo IO库是进口的)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Shirt Colors</title> 

     <!-- link to dojo online --> 
     <script src="//ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"></script> 

     <!-- javascript file --> 
     <script type = "text/javascript" src = "shirtColors.js"></script> 

     <!-- import the dojo io library --> 
     <script type = "text/javascript"> 
     dojo.require("dojo.io.*"); 
     </script> 

    </head> 
    <body> 

     <select onchange = "getColors(this.value);"> 
      <option value = "large"> 
       large shirt 
      </option> 
      <option value = "medium"> 
       medium shirt 
      </option> 
      <option value = "small"> 
       small shirt 
      </option> 
     </select> 

     <select id = "colorselect"></select> 

    </body> 
</html> 

回答

2

dojo.io.bind是一个很旧的函数。

新和支持的方式为你的用例是使用dojo.xhrGet或dojo.xhrPost

var deferred = dojo.xhrGet(
{ 
    url: "shirtColors.php", 
    content: { 
     'size': size 
    }, 
    handleAs: "text", 
    load: buildMenu, 
    error: function(error){ 
     //error handling code 
    } 
    } 


); 

注意查询参数为“内容”被传递。

修改您的buildMenu功能:

function buildMenu (data) 
{ 
//data contains the text string for the data returned by PHP 
} 

更多细节

+0

啊,感谢您的帮助,现在的作品!除了 - 仅在菜单中设置从PHP文件回显的字符串的第一部分(在**第一个逗号之前的**)(例如:“黑色”为“黑色,橙色,灰色”)时, 。我是否可能有错字,或者我是否错误地应用了Dojo? – 2012-04-03 07:02:03

+0

很可能剩余的数据不在响应文本中。您可以通过单击此请求的Firebug Net选项卡中的响应选项卡来检查响应。或者在浏览器中单独尝试此URL。如果仍然是问题,我建议以JSON格式返回数据,因为它比纯文本更具结构。 – 2012-04-03 13:46:22

1

http://dojotoolkit.org/reference-guide/1.7/dojo/xhrGet.html和更改这些文件的地方。如果shirtolors依赖dojo.io它不会被加载。

<!-- import the dojo io library --> 
     <script type = "text/javascript"> 
     dojo.require("dojo.io.*"); 
     </script> 



<!-- javascript file --> 
     <script type = "text/javascript" src = "shirtColors.js"></script>