使用Ajax长轮询从外部API更新我页面上的响应 [英] Using ajax long polling to update a response on my page from an external API
问题描述
我有以下 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发出请求.
When I render the page, the ajax script runs and waits for response. When I checked the network tab, ajax was endlessly making requests to the URL specified.
我从外部网站到后端PHP收到一个表单发布,我需要将其推送到jquery.
I get a form post from an external website to the backend PHP which I need to push to the jquery.
但是,当发生发布时,控制台中没有任何日志记录.但是,如果我在 $ json 中硬编码一些值并回显它,则它会出现在控制台中.
But when a post is happening, nothing is being logged in the console. But if I hard code some values in the $json and echo it, its coming up in the console.
我面临两个问题
-
在PHP脚本上发生帖子时,它不会出现在ajax代码中.
When a post happens on the PHP script, its not coming up in the ajax code.
当我对 $ json 进行硬编码(模拟外部表单发布的响应)并回显它时,它会出现在控制台中,但数据的条件是.status =='成功'未得到检查.
When I hard code (simulated the response posted by the external form post) the $json and echo it, its coming up in the console, but the condition for data.status== 'success' is not getting checked.
这有什么问题.我想念什么吗?
What is wrong in this. Am I missing something?
更新
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.
推荐答案
如果使用长轮询,则可能会出现缓存问题. 首先,当您的帖子进入系统时,请检查checkTxn是否已更改. 最后,您可以在url查询中添加一个随机参数(例如,以millis为单位添加日期),虽然您不会使用它,但是您的服务器会认为您的请求有所不同.
If you are using long polling, you could have a cache issue. First, when your post comes to your system, check that checkTxn changes. Last, you can add a random parameter (by adding date in millis, for example) in url query, you will not use it, but your server will think your requests are different.
请检查并告知我们.
@Edit:当然,@ Ajeesh,我来解释一下:
@ Sure @Ajeesh, I'll explain it:
(function poll(){
$.ajax({ url: "<?php echo URL::to('/internal/v1/checkTxn'); ?>?_ts=" + new Date().getTime(), 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 });
})();
这样做,将不会使用缓存,因为服务器/浏览器的所有查询都不同.
By doing this, cache will not be used, because all queries are different for your server/browser.
另一方面,当您收到POST时,我要求您对页面进行任何更改,因此,如果没有,您的ajax将永远不会收到通知,您知道我的意思吗?
On the other hand, I ask you for any changes in your page when you receive the POST, so, if not, your ajax will never receive the notification, do you know what I mean?
这篇关于使用Ajax长轮询从外部API更新我页面上的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!