[英]Can't seem to get proper response from my request
Here is the ajax/dojo i use to call my PHP file, request to get data from the database table: 这是我用来调用PHP文件,请求从数据库表获取数据的ajax / dojo:
var _getWeatherInfo = function(){
dojo.xhrget({
url: "PHP/weather.php?ntown=" + _ntown,
handleAs: "json",
timeout: 5000,
response: function(response, weather_info) {
_refreshWeatherList
},
error: function(error_msg, weather_info) {
_handleError(error_msg);
}
});
}
The next bit of code below here is what i use to look through the returned json array object (which is now as "weather_info" in the javascript file) and add the data from the object to my javascript array called "_weather". 下面的代码是我用来查看返回的json数组对象(现在在javascript文件中为“ weather_info”)并将该对象中的数据添加到名为“ _weather”的javascript数组中的代码。
var _refreshWeatherList = function(weather_info) {
for (var i = 0; i < weather_info.length; i++) {
_weather.push(weather_info[i]);
}
}
MY PROBLEM IS: 我的问题是:
I dont think I am actually getting a proper response from my PHP request, as when I run my app and click the button i've used to alert the "_weather" array nothing is displayed. 我认为我实际上没有从我的PHP请求中得到正确的响应,因为当我运行我的应用程序并单击我用来提醒“ _weather”数组的按钮时,什么都没有显示。 How can I change my code so it properly gets a response from my request.
如何更改我的代码,使其正确地从我的请求中得到响应。
Thanks for any help. 谢谢你的帮助。
EDIT: PHP: 编辑:PHP:
<?php
$ntown = $_GET['ntown'];
$weather = array();
$query="SELECT * FROM `weather` WHERE `town` = '$ntown'";
$result=mysql_query($query) or die ("Query to get data from table failed: ".mysql_error());
while($row = mysql_fetch_row($result)) {
$weather = $row;
}
echo json_encode($weather);
mysql_close();
?> ?>
Unless this is just a typo, I think your problem is in this bit: 除非这只是一个错字,否则我认为您的问题就在于此:
response: function(response, weather_info) {
_refreshWeatherList
},
Try changing it to: 尝试将其更改为:
load: function(response) {
_refreshWeatherList(response);
},
// or just load: _refreshWeatherList
To debug problems like this, learn to use your browser's developer tools. 要调试此类问题,请学习使用浏览器的开发人员工具。 In Firefox, use Tools -> Web developer -> Web console, or in Chrome click F12 and select the Network tab.
在Firefox中,使用工具-> Web开发人员-> Web控制台,或在Chrome中单击F12,然后选择网络标签。 (Other browsers usually have similar tools.) These will tell you about any requests being made, and clicking on the request will allow you to see the response, headers etc.
(其他浏览器通常也使用类似的工具。)这些工具将告诉您正在发出的任何请求,单击该请求将使您可以看到响应,标头等。
The issue with the php code: 的PHP代码的问题:
$weather = $row;
should be 应该
$weather[] = $row;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.