简体   繁体   English

Ajax请求从php文件获取数据?

[英]Ajax request to get data from php file?

I have been researching on how to make an Ajax request and came up with this: 我一直在研究如何提出Ajax请求,并提出了以下建议:

function ajax_post(){
    // Create our XMLHttpRequest object
    var xmlhttp = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "http://localhost:888...-files/test.php";
    // Set content type header information for sending url encoded variables in the request
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    xmlhttp.onreadystatechange = function() {
   if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   var return_data = xmlhttp.responseText;
document.getElementById("demo").innerHTML = return_data;
   }
    }
    xmlhttp.open("POST", url, true);
    xmlhttp.send(); // Actually execute the request
    document.getElementById("demo").innerHTML = "processing...";
}

I saved this script as a file under: http://localhost:888...ascript/test.js <-- I have tested other scripts saved at this location and they work perfectly fine. 我将此脚本另存为以下文件: http:// localhost:888 ... ascript / test.js <-我已经测试了保存在此位置的其他脚本,它们运行良好。 My php file contains the following data (it is named "test.php"): 我的php文件包含以下数据(名为“ test.php”):

<?php
echo(rand(10,100));
?>

After I make the request to the php file which should display a random number according to the php code, my html looks like this: 在我向php文件发出请求后,该文件应根据php代码显示一个随机数,我的html如下所示:

<div style="display:none;">
<body onload="ajax_post()">     <------ Here you can see that I have called the function which executes the AJAX Request after the page has loaded.
<script type="text/javascript" src="http://localhost:888...ascript/test.js"></script>
</body>
</div>
<div id="demo"></div>

I keep refreshing the page and nothing appears. 我一直在刷新页面,但没有任何显示。 Does it have to do with my php code? 它与我的php代码有关吗? Perhaps my Ajax request is wrongly structured? 也许我的Ajax请求结构错误? In terms of Ajax request, I have tried "GET" and "POST" as well and still nothing happens. 在Ajax请求方面,我也尝试过“ GET”和“ POST”,但仍然没有任何反应。 I am new at Ajax and the might syntax my not make sense... Thank you in advance for the support. 我是Ajax的新手,可能使用的语法没有意义……谢谢您的支持。

Regards!! 问候!!

I added some html to the php file and everything loads except the php file (php appeared as a comment). 我在php文件中添加了一些html,除了php文件(php作为注释显示)之外,其他所有内容均已加载。 Then, I came up with the idea to compare the php file I created with the other php files on my local server. 然后,我想到了将我创建的php文件与本地服务器上的其他php文件进行比较的想法。 I identified a little difference. 我发现了一点区别。 The other php files do not close the php, ie <?php echo 'Hello World'; 其他php文件不会关闭php,即<?php echo 'Hello World'; without putting ?> at the end, and now it works. 不用在末尾加上?> ,现在就可以了。 My php file looks like this: 我的php文件如下所示:

<?php
echo 'Hello World';

I also simplified my script, making my structure look like follows: 我还简化了脚本,使结构如下所示:

<div id="demo"><div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready( function() {
$('#demo').load('http://localhost:8888/...php-files/test.php');
});
</script>

I used JQuery .load() property as it is simpler than the normal script (same principle). 我使用了JQuery .load()属性,因为它比普通脚本更简单(原理相同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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