1. I want to send ajax and use php to return a json data, but even the string "Hellow world" cannot be returned
2. Upload the code
js code
<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 code
<?php
header("Content-Type:text/html;charset=UTF-8");
echo "Hello,World!!!";
?>
Error message:
Submit address bar: http://heima.com/Coder/�...
Print results:
Before request is initiated
<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. I’m not sure what’s wrong. Please help me take a look. If hello world can be returned, can the following json data be returned?
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);
?>
According to the error report, it seems that the access path (file) does not exist. The answer above is very clear.
You can check it like this, assuming that your Html file (should contain your js code) is in
$_PATH/test.html
, and php is thedata.php
file1. Check whether the file
$_PATH/data.php
exists in the current directory2. Manually access the local environment (such as: http://localhost/data.php) to know whether it is normal (if an error is reported, then The problem has been found)
3. It is not recommended to write the ajax
url: "./data.php"
like this, justurl: "data.php"
4. It is not recommended to use Chinese naming for folder names. This must be changed!
Error message
No such file or directory in Unknown on line 0
Fatal error: Unknown: Failed opening required 'F:/heima/Coder/Testing Field/Ajax/data.php
Your file path is wrong.
First question
Your HTML code returns the same, which is actually PHP’s error message (formatted error message):
Second questionIf you access the page directly, you can intuitively see the following error:
This is because you The file
include
'F:/heima/Coder/Testing Field/Ajax/data.php'does not exist. Please check first whether this file actually exists on your PHP server. It is also recommended to give the file structure and whether the HTML is opened locally or mounted on the server and opened from the HTTP protocol, so as to facilitate more detailed analysis and give more accurate suggestions.
I took a look at your header content, which is the status information returned by the browser in F12. You should know it. There is a request URL in it, which probably means that your request address is wrong. It's just that bunch of garbled stuff that shouldn't appear. (In this case, I changed the request method or the imported jquery. I can’t remember clearly. You can try both.) I took a screenshot and you can take a look
. . . .
I have also encountered this reason, but I seem to be using jquery (probably, I can’t remember clearly). Another reason is that the format of your ajax is incorrect. I won’t post the specific format. You can find a usable ajax format and send it. The post and get formats must correspond. Also, this is the first time I have seen type:get put at the front. It’s not that it’s wrong, it just feels awkward. That’s all, I hope it helps you
It is indeed a problem with the php path error. The reason is that there is an error in Chinese on the file path when parsing the php address. Now it is ok and can be accessed normally. Thank you everyone