javascript - 发送ajax返回用php返回数据不成功

查看:90
本文介绍了javascript - 发送ajax返回用php返回数据不成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

1.想发ajax用php返回一个json数据,但是连字符串"Hellow world"都返回不了

2.上代码

js代码

<script src="./jquery.js"></script>
<script>
$.ajax({
    type:'get',
    url:'./data.php',

    beforeSend:function(){
        console.log("请求发起前")
    },
    success:function(result){
        console.log(result);
    },
    complete:function(){
        console.log('请求完成');
    }
});
</script>

php代码

<?php
header("Content-Type:text/html;charset=UTF-8");

echo "Hello,World!!!";
?>

报错信息:
提交地址栏:http://heima.com/Coder/%E6%B5...

打印结果:
请求发起前

<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: Unknown: failed to open stream: No such file or directory in Unknown on line <i>0</i></th></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Unknown: Failed opening required 'F:/heima/Coder/测试场/Ajax/data.php' (include_path='.;C:\php\pear') in Unknown on line <i>0</i></th></tr>
</table></font>
 请求完成

3.不清楚哪儿有错呢,大家帮忙看一下吧,如果可以返回hello world 那么下面的json数据可以返回吗?

php (json)

<?php
header('Content-type: text/json');
$fruits = array (
    "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),
    "numbers" => array(1, 2, 3, 4, 5, 6),
    "holes"   => array("first", 5 => "second", "third")
);
echo json_encode($fruits);
?>

解决方案

根据报错来看是访问路径(文件)不存在,这块楼上都回答的很清楚了。

可以这么排查,假设你的Html文件(应含你的js代码)在$_PATH/test.html,php为data.php文件

1、检查当前目录是否存在$_PATH/data.php这个文件
2、手动访问本地环境下(如:http://localhost/data.php),就知道是否正常了(如果报错那就已经找到问题了)
3、不建议ajax的url:"./data.php"这么写,直接 url:"data.php" 即可
4、不建议文件夹名字使用中文命名,这个一定要改!

这篇关于javascript - 发送ajax返回用php返回数据不成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆