- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下ajax长轮询脚本
(function poll(){
$.ajax({ url: "<?php echo URL::to('/internal/v1/checkTxn'); ?>", success: function(data){
//Update your dashboard gauge
console.log(data.status); //Data is getting logged
if(data.status == 'success'){ //This condition is not being checked
console.log('suucesful'); //Not coming
}
}, dataType: "json", complete: poll, timeout: 1000 });
})();
后端PHP代码如下
if(isset($_POST['status']) && $_POST['status']){
$data = ['status'=>$_POST['status']];
$json = json_encode( $data );
echo $json;
}
流量
当我渲染页面时,ajax 脚本运行并等待响应。当我检查网络选项卡时,ajax 无休止地向指定的 URL 发出请求。
我从外部网站向后端 PHP 发送了一个表单帖子,我需要将其推送到 jquery。
但是当帖子发生时,控制台中没有记录任何内容。但是,如果我在 $json 中硬编码一些值并回显它,它就会出现在控制台中。
我面临两个问题
当 PHP 脚本上发生帖子时,它不会出现在 ajax 代码中。
当我对 $json 进行硬编码(模拟外部表单发布的响应)并回显它时,它会出现在控制台中,但是 data 的条件.status==“成功”未得到检查。
这有什么问题吗?我错过了什么吗?
更新
I could fix the "condition not being checked" as there was something wrong the json being echoed.
Now to avoid confusion, the flow for this
User open the page,
> The ajax starts the long polling process to my PHP code, waiting for a
> response.User enters payment details in a form which is my html,clicks on pay, a pop up appears
> which renders the banks login page (Payment gateway).After user entering all
> details in the pop up (banks page), bank sents a server to server call about the status of
> transaction to my notificationURL
> ('mydomain.com/internal.v1/checkTxn'). As soon as I get a POST on this
> URL(I will close the pop up), my ajax polling should get the data posted to my PHP and there by
> I will show the status of TXN to the user on the same card form he entered his details earlier and
> the pop window closes. The response here is returned by my PHP code to the ajax.
The
> post coming to my PHP code is a server to server post which is posted
> by a Payment Gateway.
最佳答案
<强>1。让我们调试一下:
设置你的ajax错误回调,
$(function(){
(function poll(){
$.ajax({ url: "http://tinyissue.localhost.com/test.php", success: function(data){
//Update your dashboard gauge
console.log(data.status); //Data is getting logged
if(data.status == 'success'){ //This condition is not being checked
console.log('suucesful'); //Not coming
}
},error:function(err){
console.info('error fired...');
console.info(err);
}, dataType: "json", complete: poll, timeout: 1000 });
})();
});
运行这个,你将得到控制台
error fired...
Object {readyState: 4, responseText: "", status: 200, statusText: "OK"}
<强>2。为什么会出现错误回调:
为什么ajax响应status
是200并且statusText
是“OK”,错误
回调仍然被触发而不是 success
?
您的 AJAX 请求包含以下设置:
dataType: "json"
documentation声明 jQuery:
Evaluates the response as JSON and returns a JavaScript object. (...) The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.
这意味着,如果服务器返回无效 JSON 且状态为 200 OK,则 jQuery 会触发错误函数并将 textStatus 参数设置为“parsererror”。
解决方案:确保服务器返回有效的 JSON。值得注意的是,空响应也被视为无效 JSON;例如,您可以返回 {} 或 null,其验证为 JSON。
<强>3。为什么ajax返回无效的JSON:
在您看来,在服务器端,您检查了 $_POST['status']
以确保循环轮询中最后一次调用成功,仅 $_POST['status']
设置后,您将回显 json,否则什么也不回显。
但是,不幸的是,在调用循环开始时,第一次 ajax 调用时,您没有将 status
设置为发布。然后它什么也不回显,然后它进入 error
回调,也进入 complete
回调,然后在没有 status
发布的情况下再次调用。看,这是一个糟糕的循环。
<强>4。解决方案:
设置要在第一次 ajax 调用时发布的 status
值。
$(function(){
(function poll(){
var status = 'success';
$.ajax({ url: "http://tinyissue.localhost.com/test.php", success: function(data){
//Update your dashboard gauge
console.log(data.status); //Data is getting logged
status = data.status;
if(data.status == 'success'){ //This condition is not being checked
console.log('suucesful'); //Not coming
}
},error:function(err){
console.info('error fired...');
console.info(err);
status = 'error';
}, type:'post',dataType: "json", data:{status:status}, complete: poll, timeout: 1000 });
})();
});
关于javascript - 使用 ajax 长轮询从外部 API 更新我的页面上的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37129486/
var status = {{ check_status }}; call_polling = function() { // polling request to API (async)
我有一个网络应用程序,它有一个计时器,每 3 秒触发一次轮询以获取数据。它工作正常大约 2.5 分钟,然后 Chromium 崩溃。 我的请求 Dart 看起来像这样 HttpRequest.getS
是否已经有为 jQuery 编写的通知库?一种提供下拉消息的方式,类似于 StackOverflow 的方式。 寻找的东西: 1)每几秒轮询一次(也许可以选择 cometd ) 2) 有一个标记为已读
当电池电量不足时,Android 将发送一个 ACTION_BATTERY_LOW Intent 。然后当它再次正常时,它会发送 ACTION_BATTERY_OKAY。 不幸的是,如果我的应用程序在
如果在内核模式CPU可以访问用户空间内存,为什么我们在调用poll/select时需要将数据从用户空间复制到内核空间? 引自 Linux 编程接口(interface): When running i
我正在尝试使用来自 python 的 poll() 来轮询一个进程。 link 我不明白,我正在为它提供一个整数值来进行轮询。例如:值为 14870 14870 Traceback (most rec
我正在用 Java 编写一个程序,我在一个线程上打开了 256 个网络连接。每当套接字上有任何数据时,我都应该读取它并进行处理。目前,我正在使用以下方法: while true do itera
我有一个有点哲学的问题。我们使用存储队列来处理“门票”。我们实现的方式是我们有一个后台服务(辅助角色),它轮询存储队列并找出是否有任何票证需要处理。我们所做的工作具有季节性。这意味着不会一直有票需要处
我目前正在使用 CFReadStreamHasBytesAvailable 轮询我的 CFReadStream 以获取新数据。 (首先,一些背景知识:我正在做自己的线程,我不想/不需要搞乱运行循环的东
使用 azure-sdk-for-php 长时间轮询 Azure 队列存储时,如果我的请求间隔超过 30 秒,则库会因以下错误而终止: PHP Notice: fwrite(): send of 2
我在尝试使用 JavaScript 监听 Firebase 中的更改来进行实时刷新时遇到了一些问题。我尝试做的是一个聊天系统。与另一个用户开始新的聊天后,我将加载两个用户之间的所有聊天。 code 我
我正在开发一个使用通知系统的网站(如 Facebook 的系统)。为此,我想我将编写一个 JQuery 轮询函数,使用 ajax 在服务器端查找新通知。 我的问题是,这是个好主意吗? 最佳答案 客户端
我正在开发一个后端服务,该服务使用 spring aws 集成定期轮询 S3 存储桶,并处理来自 S3 的轮询对象。下面是它的实现 @Configuration @EnableIntegration
我想要一些关于如何实现以下内容的建议: 我想让我的用户通过 AJAX 了解在我的服务器上运行的任务的进度。我的服务器运行一个 PHP 脚本,该脚本使用 popen 函数通过 shell 命令下载文件。
我想使用 jQuery 和 AJAX 持续轮询 URL,直到收到 200 状态代码响应。 URL 指向提供文件的 REST API。理想情况下,我会获取其他状态代码,在这些状态代码上我将再次调用 UR
我想知道是否可以将 Ajax 轮询插入到我下面的当前代码中,以便用户每隔几秒发布一次更新,以显示添加到数据库中的任何新内容,例如它们的状态和新的数量添加的评论和我构建的提要中的时间戳。这是我到目前为止
我想弄清楚如何在不使用触发器的情况下轮询对 Oracle 表所做的更改。我目前关心的唯一变化是新的/插入的记录。任何建议将不胜感激。 我也真的不想使用其他表来跟踪更改的内容。 谢谢! 小号 最佳答案
这个问题在这里已经有了答案: How do I return the response from an asynchronous call? (41 个回答) 关闭 3 年前。 我正在尝试轮询 AP
我有一个 SSIS 包,它在 Foreach 容器 中启动另一个 SSIS 包;因为容器在启动它必须启动的所有包后立即报告完成,我需要一种方法让它等到所有“子”包都完成。 所以我实现了一个小的 sle
我将这段代码放入我的主视图文件中: jQuery(document).ready(
我是一名优秀的程序员,十分优秀!