Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么php ajax没有给出任何响应?_Javascript_Php_Ajax - Fatal编程技术网

Javascript 为什么php ajax没有给出任何响应?

Javascript 为什么php ajax没有给出任何响应?,javascript,php,ajax,Javascript,Php,Ajax,我正在做我的大学项目,因为我有一个显示城市和温度的网页,我尝试使用AJAX每隔一段时间更新温度。 这是密码 对于ajax请求Java脚本,我在回答中看到了这段代码 警报(“Ajax响应打开”+qry)工作正常,因此我知道此代码正在运行。但是当我把这种“alert()”放在dispatchCallBack(qry,response)之前时它不起作用,因此我推断请求没有成功 下面是ajax\u resp.php <?php if(isset($_POST['q'])&&!em

我正在做我的大学项目,因为我有一个显示城市和温度的网页,我尝试使用AJAX每隔一段时间更新温度。 这是密码

对于ajax请求Java脚本,我在回答中看到了这段代码

警报(“Ajax响应打开”+qry)工作正常,因此我知道此代码正在运行。但是当我把这种“alert()”放在
dispatchCallBack(qry,response)之前时它不起作用,因此我推断请求没有成功

下面是
ajax\u resp.php

<?php

if(isset($_POST['q'])&&!empty($_POST['q'])
{
    if($_POST['q']=="c_temp")
    {
        $api_key = "Here's my api key, working alright"; 
        $city_id = "here's city id, also working correct";
        $url = "http://api.openweathermap.org/data/2.5/forecast/city?id=".$city_id."&APPID=".$api_key;

        $json = file_get_contents($url);
        $json_o = json_decode($json);

        $city_name =  $json_o->city->name;
        $temp_c =  $json_o->list[0]->main->temp-273;
        echo json_encode(array("city"=>$city_name,"temp"=>$temp));
//I have checked JSON response seperately its correct. I was able to get city name and temperature variable properly initialised.
    }
}?>

请查看下面的代码并更正它们

[1] 使用单引号或双引号从具有字符串索引的
$\u POST
变量获取数据

[2]
$qry
之前未定义。阅读问题后,$qry的最可能值可以是$_POST['q']。因此,将
$qry
替换为
$\u POST['q']
或分配
$qry=$\u POST['q']
if(isset($_POST['q'])&&!empty($_POST['q'])
{
if($_POST['q']=="c_temp") <==  $_POST['q'] instead of $qry
{
    $api_key = "Here's my api key, working alright"; 
    $city_id = "here's city id, also working correct";
    $url = "http://api.openweathermap.org/data/2.5/forecast/city?id=".$city_id."&APPID=".$api_key;

    $json = file_get_contents($url);
    $json_o = json_decode($json);

    $city_name =  $json_o->city->name;
    $temp_c =  $json_o->list[0]->main->temp-273;
    echo json_encode(array("city"=>$city_name,"temp"=>$temp));
    //I have checked JSON response seperately its correct. I was able to get city name and temperature variable properly initialised.
}
}?>
if(设置($\u POST['q'])和&!空($\u POST['q']))
{
如果($_POST['q']==“c_temp”)城市->名称;
$temp_c=$json_o->list[0]->main->temp-273;
echo json_encode(数组(“city”=>$city_name,“temp”=>$temp));
//我分别检查了JSON响应是否正确。我能够正确初始化城市名称和温度变量。
}
}?>

请查看下面的代码并更正它们

[1] 使用单引号或双引号从具有字符串索引的
$\u POST
变量获取数据

[2]
$qry
之前没有定义。阅读问题后,$qry的最可能值可以是$\u POST['q']。因此,将
$qry
替换为
$\u POST['q']
或分配
$qry=$\u POST['q']
if(isset($_POST['q'])&&!empty($_POST['q'])
{
if($_POST['q']=="c_temp") <==  $_POST['q'] instead of $qry
{
    $api_key = "Here's my api key, working alright"; 
    $city_id = "here's city id, also working correct";
    $url = "http://api.openweathermap.org/data/2.5/forecast/city?id=".$city_id."&APPID=".$api_key;

    $json = file_get_contents($url);
    $json_o = json_decode($json);

    $city_name =  $json_o->city->name;
    $temp_c =  $json_o->list[0]->main->temp-273;
    echo json_encode(array("city"=>$city_name,"temp"=>$temp));
    //I have checked JSON response seperately its correct. I was able to get city name and temperature variable properly initialised.
}
}?>
if(设置($\u POST['q'])和&!空($\u POST['q']))
{
如果($_POST['q']==“c_temp”)城市->名称;
$temp_c=$json_o->list[0]->main->temp-273;
echo json_encode(数组(“city”=>$city_name,“temp”=>$temp));
//我分别检查了JSON响应是否正确。我能够正确初始化城市名称和温度变量。
}
}?>


添加
。捕获
回调并查看它是否失败您在浏览器的开发人员工具中查看了AJAX请求/响应吗?您是否在项目中包含了jQuery库?是否报告了任何错误?是否在web服务器上运行此操作?首先打开浏览器的开发人员工具,查看网络活动。这将使您知道问题是在客户端还是服务器上。如果是服务器问题,则会看到错误响应。如果是客户端问题,则根本不会发出任何请求。此
$\u POST[q]
应为
$\u POST['q']
,您需要分配
$qry=$\u POST['q']
firstadd
。捕获
回调并查看它是否失败您在浏览器的开发人员工具中查看了AJAX请求/响应吗?您是否在项目中包含了jQuery库?是否报告了任何错误?您是否在web服务器上运行此操作?首先打开浏览器的开发人员工具,查看网络活动。这将导致我会让您知道问题是在客户端还是服务器上。如果是服务器问题,则会看到错误响应。如果是客户端问题,则根本不会发出任何请求。此
$\u POST[q]
应为
$\u POST['q']
,您需要分配
$qry=$\u POST['q']
首先,OP为什么要尝试这个?一个好的答案总是会解释做了什么以及为什么这样做,不仅是为了OP,而且是为了SO的未来访问者。@JayBlanchard这听起来不错吗?好的,我已经做了一些更改,我想我计划使用$qry,但错过了,不管怎样,还是一样,任何其他建议,代码还有什么问题吗?@Salim write
console.log(response)
查看如果我删除所有php代码,只删除硬代码
echo json_encode(数组(“city”=>“London”,“temp”=>32))会从php文件返回什么
那么它也不起作用了为什么OP要尝试这个?一个好的答案总是会有一个解释,说明做了什么以及为什么这样做,不仅是为了OP,而且是为了以后的访客。@JayBlanchard听起来不错吗?好吧,我已经做了一些更改,我想我计划使用$qry,但还是错过了,无论如何,还是一样,一个还有其他建议,代码还有什么问题吗?@Salim write
console.log(response)
查看php文件返回的内容实际上如果我删除所有php代码,只使用硬代码
echo json_encode(数组(“city”=>“London”,“temp”=>32));
那么它也不起作用
if(isset($_POST['q'])&&!empty($_POST['q'])
{
if($_POST['q']=="c_temp") <==  $_POST['q'] instead of $qry
{
    $api_key = "Here's my api key, working alright"; 
    $city_id = "here's city id, also working correct";
    $url = "http://api.openweathermap.org/data/2.5/forecast/city?id=".$city_id."&APPID=".$api_key;

    $json = file_get_contents($url);
    $json_o = json_decode($json);

    $city_name =  $json_o->city->name;
    $temp_c =  $json_o->list[0]->main->temp-273;
    echo json_encode(array("city"=>$city_name,"temp"=>$temp));
    //I have checked JSON response seperately its correct. I was able to get city name and temperature variable properly initialised.
}
}?>