$.get(url, function (response) {})中将获取信息进行同步请求
时间: 2024-02-18 11:50:57 浏览: 13
还是异步请求呢?为什么?
这是一个关于 Ajax 请求的问题。$.get()是 jQuery 提供的一个函数,用于向指定的 URL 发起 GET 请求,并使用获取到的数据执行指定的回调函数。$.get()默认是异步请求,意味着程序发出请求后不会阻塞后续代码的执行,而是继续执行后续代码,等到请求返回数据后再执行回调函数。这样可以提高页面的响应速度。如果需要将请求改为同步,可以设置async:false,但这样会导致页面长时间等待,影响用户体验,所以不建议使用同步请求。
相关问题
$(document).ready(function(){ $("#openBtn").click(function(){ $.ajax({ url:"ntp.php", type:"GET", success:function(response){ $("#fileContent").val(response); } }); }); <?php $process = shell_exec("ps aux | grep 'ntpd' | grep -v 'grep'"); if($process){ echo 'NTP服务正在运行'; }else{ echo 'NTP服务没有运行'; } $file_path = "/etc/ntp.conf"; $file_content = file_get_contents($file_path); $file_content1 = $_POST['content']; file_put_contents($file_path,$file_content1); ?> 修改代码 点击openBtn 只获取/etc/ntp.conf的内容
你的代码中已经有了获取/etc/ntp.conf文件内容的部分,即在Ajax请求的success函数中将响应内容赋值给#fileContent元素的值。因此,只需要修改Ajax请求的URL为"/etc/ntp.conf"即可。
修改后的代码如下:
```
$(document).ready(function(){
$("#openBtn").click(function(){
$.ajax({
url:"/etc/ntp.conf",
type:"GET",
success:function(response){
$("#fileContent").val(response);
}
});
});
});
```
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> <div id="content"></div> <script> function getQueryString(name) { varreg =new RegExp('(^|&)'+ name +'=([^&]*)(&|$)','i'); varr = window.location.search.substr(1).match(reg); if(r !=null) { returnunescape(r[2]); } returnnull; } $.get('https://4147093qp2.imdo.co/lt_data/'+getQueryString('')+'.md', function(response, status, xhr){ $("#content").html(marked.parse(response)); }); </script>报错:Uncaught ReferenceError: reg is not defined at getQueryString
这个错误是由于在 `getQueryString` 函数中 `varr` 的声明中使用了错误的变量名称 `reg`,应该是 `var reg`。
你可以在代码中将 `varr` 更正为 `var r`,例如:
```javascript
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
```
这将解决 `Uncaught ReferenceError: reg is not defined` 错误。
如果更正后仍然存在问题,请确保在引用 `marked.min.js` 库前已经引用了 `jquery-3.4.1.min.js` 库,并且 `getQueryString` 函数在 `$.get` 函数调用之前被定义。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)