- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于php来说完全是新的/ajax 。每个教程似乎都以某种方式过时了。花了大约 30 个小时试图想出一个简单的插入/检索脚本。请帮忙!一旦我看到代码工作起来,我就更容易摆弄它并理解文档。
我收到以下错误。
[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
我的index.php相关数据,错误在xmlhttp.open
行。
disp_data();
function disp_data(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "update.php?status=disp",false);
这是我的update.php相关代码。在教程中,它应该在刷新时加载我的数据,但它是空白的。我不知道为什么教程将其设置为 false,当我在 w3schools 阅读的文档似乎我应该将其设为 true,但两者都不起作用。
$status = $_GET["status"];
if ($status == "disp") {
$link = mysqli_connect("localhost", "root", "");
mysqli_select_db($link, "checkbox");
我的完整index.php
<div id ="disp_data"></div>
<script src="jquery-3.2.1.min.js"></script>
<script type ="text/javascript">
(function() {
var newXHR = null;
function sendXHR(type, url, data, callback) {
newXHR = new XMLHttpRequest() || new window.ActiveXObject("Microsoft.XMLHTTP");
newXHR.open(type, url, true); // Use async = true to avoid bad user experience for waiting a Sychronous request that might block the UI.
newXHR.send(data);
newXHR.onreadystatechange = function() {
if (this.status === 200 && this.readyState === 4) {
callback(this.response); // Callback function to process the response.
}
};
}
sendXHR("GET", "update.php?status=disp", null, function(response) {
document.getElementById("disp_data").innerHTML=newXHR.responseText;
});
})();
</script>
</body>
我的完整 update.php 文件
$status = $_GET["status"];
if($status=="disp")
{
$link = mysqli_connect("localhost", "root", "");
mysqli_select_db($link,"checkbox");
$res = mysqli_query($link,"SELECT * FROM table1");
echo "<table>";
while($row = mysqli_fetch_array($res))
{
echo "<tr>";
echo "<td>"; echo $row["id"]; echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "<td>"; echo $row["city"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
}
最佳答案
您应该在 async
参数中使用 true
。
类似这样的:xmlhttp.open("GET", "update.php?status=disp", true);
。
您可以使用此辅助函数来生成 XMLHttpRequest
。
适用于所有浏览器,包括 IE6。
var newXHR = null;
function sendXHR(type, url, data, callback) {
newXHR = new XMLHttpRequest() || new window.ActiveXObject("Microsoft.XMLHTTP");
newXHR.open(type, url, true); // Use async = true to avoid bad user experience for waiting a Sychronous request that might block the UI.
newXHR.send(data);
newXHR.onreadystatechange = function() {
if (this.status === 200 && this.readyState === 4) {
callback(this.response); // Callback function to process the response.
}
};
}
用法:
sendXHR("GET", "update.php?status=disp", null, function(response) {
console.log(response);
});
您可以使用此演示检索数据:
(function() {
var newXHR = null;
function sendXHR(type, url, data, callback) {
newXHR = new XMLHttpRequest() || new window.ActiveXObject("Microsoft.XMLHTTP");
newXHR.open(type, url, true); // Use async = true to avoid bad user experience for waiting a Sychronous request that might block the UI.
newXHR.send(data);
newXHR.onreadystatechange = function() {
if (this.status === 200 && this.readyState === 4) {
callback(this.response); // Callback function to process the response.
}
};
}
// Example 1: Get data.
var btnRequest = document.getElementById("btnRequest");
btnRequest.onclick = function() {
sendXHR("GET", "https://gist.githubusercontent.com/dannyjhonston/22851f9c21733973b2705b0b65443f90/raw/30cf99ceeb470a7ab6d2ffb51a78f1bb57f41ca3/data.txt", null, function(response) {
document.getElementById("myDiv").innerHTML = response; // response contains the data from the server.
});
};
// Example 2. Cancel a long request.
var btnCancelRequest = document.getElementById("btnCancelRequest");
btnCancelRequest.onclick = function() {
newXHR.abort(); // You can use the newXHR variable to abort a XMLHttpRequest that is taking long time to response, with the .abort() method.
};
})();
#myDiv {
border: solid 1px #ccc;
border-radius: 5px;
margin: 20px;
padding: 5px;
}
<button id="btnRequest" type="button">Request data</button>
<button id="btnCancelRequest" type="button">Cancel request</button>
<div id="myDiv">The new content will print here.</div>
很高兴知道这一点:
<小时/>
XMLHttpRequest.response
property returns the response's body. It can be of the typeArrayBuffer
,Blob
,Document
,JavaScript
object, or aDOMString
, depending of the value ofXMLHttpRequest.responseType
property. Value of response is null if the request is not complete or was not successful. However, if the value ofresponseType
was set to"text"
or the empty string, response can contain the partial text response while the request is still in the loading state.
XMLHttpRequest.responseText
property returns aDOMString
that contains the response to the request as text, ornull
if the request was unsuccessful or has not yet been sent. TheresponseText
property will have the partial response as it arrives even before the request is complete. IfresponseType
is set to anything other than the emptystring
or"text"
, accessingresponseText
will throwInvalidStateError
exception.
您的php代码必须是这样的:
$status = $_GET["status"];
if($status=="disp")
{
$link = mysqli_connect("localhost", "root", "");
mysqli_select_db($link,"checkbox");
$res = mysqli_query($link,"SELECT * FROM table1");
$html = "<table>"; // Declare a variable to concat the html content.
while($row = mysqli_fetch_array($res))
{
$html += "<tr><td>";
$html += $row["id"];
$html += "</td><td>";
$html += $row["name"];
$html += "</td><td>";
$html += $row["city"];
$html += "</td></tr>";
}
$html += "</table>";
echo $html; // Prints the $html variable.
}
在你身上html您需要删除的代码:
<script src="jquery-3.2.1.min.js"></script>
如果你要使用原生的 javascript 则没有必要.
关于javascript - 在ajax中调用PHP函数,不推荐使用同步XMLHttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47321853/
我只需要以下代码的清晰解释来创建 XMLHttpRequest . var xhr = false; if (window.XMLHttpRequest) { xhr = new XMLHtt
我无法让 XMLHttpRequest 对象在我正在编写的仪表板小部件中正常工作。我将其隔离为一个在 main.js 文件的全局范围内不起作用的简单示例: xhr = new XMLHttpR
首先,如果我要说的没有任何意义,请允许我道歉。我已经离开了我的舒适区,我真的不知道我在做什么。照这样说... 我正在使用 Google Drive File Picker library允许用户从他们
我还有一个问题。 XMLhttpRequests 困扰着我。一切现在都在数据库中,但我需要这些数据来更新我的页面以加载或重新加载第一页。 XHR 在触发 PHP 脚本的 JavaScript 文件中触
我试图了解从 WebAssembly 调用 XmlHttpRequest 的最佳和最有效的方法是什么。 我找到了 http://webassembly.org/getting-started/js-a
我有兴趣只提取 XHR 的网址,而不是网页中的每个网址: 这就是我提取页面中每个 url 的代码: import scrapy import json from scrapy.selector imp
有没有办法检测 XmlHttpRequest 无法解析 URL?使用错误的 URL 调用 Open(...) 时似乎不会抛出异常。 最佳答案 HTTP 状态代码将为零。 这适用于无法建立连接且未使用代
有没有办法检测 XmlHttpRequest 无法解析 URL?使用错误的 URL 调用 Open(...) 时似乎不会抛出异常。 最佳答案 HTTP 状态代码将为零。 这适用于无法建立连接且未使用代
当我在 mozilla 浏览器中运行该应用程序时,出现以下错误 主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响。更多帮助 http://xhr.spec.
我正在使用 javascript 通过 XMLHttpRequest 在 div 中加载数据,现在我想知道是否有可能在加载的 div 中创建另一个 XMLHttpRequest,这样一个新的 oncl
这个问题在这里已经有了答案: Are 'Arrow Functions' and 'Functions' equivalent / interchangeable? (4 个答案) 关闭 3 年前。
我感觉到很多关于使用新的 HTML5 JS XHR 技术的简单跨域 XmlHttpRequest 方法的讨论。给定下面的标准 JavaScript XHR 代码... var xhr=new XM
我是 PWA 的初学者,我尝试让我的代码调用 API,然后将其存储在浏览器的缓存中。但是我看到 axios 使用 XMLHttpRequest 而不是 fetch API,所以我无法缓存我的 API
这个问题在这里已经有了答案: Can we omit parentheses when creating an object using the "new" operator? (6 个答案) 关闭
这段代码块有什么区别: var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", uploadProgress,
我有一个 ajax 调用,我在其中使用 jQuery.ajax() 向 mvc 操作发出请求。这一切都很好。但是,由于某些表单具有文件控件,我将其从使用 jQuery.ajax() 更改为使用 XML
我对 Node.js 和 XMLHttpRequest 非常陌生,所以如果这是一个简单答案的问题,请耐心等待。 我目前正在尝试抓取一个 friend 的网页(当然要经过他的许可),其中包含视频和字幕。
我在 Microsoft Edge 中收到此错误。这是什么意思?在线搜索主要是为我提供有关人们在产品中遇到错误的线索。 最佳答案 如果 XMLHttpRequest 调用 ( www.mysite.s
我正在使用 jQuery 和 Office JS API 构建一个 Outlook 加载项。我在开发时有一个本地服务器正在运行,我正在尝试向我站点的主服务器上的端点提交一个 POST 请求。每次尝试提
当使用 FormData 对象将文件附加到请求时,我一直试图找到一种方法来在 XMLHttpRequest 对象上设置我自己的边界。我看过很多关于这个的帖子,每个人都说“不要设置边界,它会自动为你生成
我是一名优秀的程序员,十分优秀!