简体   繁体   中英

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:

     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".

    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. How can I change my code so it properly gets a response from my request.

Thanks for any help.

EDIT: 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. (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:

$weather = $row;

should be

$weather[] = $row;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM