简体   繁体   English

发送请求并等待响应

[英]Send request and wait for response

Is it possible in JS/php to send a request to a specific URL and then wait for responses from that URL? 在JS / php中是否可以将请求发送到特定的URL,然后等待该URL的响应?

I don't really know how to explain this question in a better way, but what I'm trying to do is to send a request to a page that has a queue on it and I want the request to "wait" on that page and send me a response as soon as it is through the queue. 我真的不知道如何更好地解释这个问题,但是我想做的是将请求发送到有队列的页面,我希望该请求在该页面上“等待”并在队列中尽快给我回复。

You can do that with AngularJS (javascript) for example. 您可以使用AngularJS(javascript)做到这一点。

With $http service you do : 使用$ http服务,您可以:

$http.get('your_url').success(function(){
// Your code if there is a good response at your request
}).error(function(){
// Your code if you detected an error (page not found etc...)
})

Official Documentation from AngularJS : $http Service AngularJS的官方文档: $ http服务

Hope that it'll help :) 希望对您有所帮助:)

with Jquery you can make an Ajax request to any other server side page in following way. 使用Jquery,您可以通过以下方式向任何其他服务器端页面发出Ajax请求。

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

With PHP you've to make an CURL call to respective server-side file 使用PHP,您必须对相应的服务器端文件进行CURL调用

No it's not possible. 不,这不可能。

When understanding you well, you send a request to a backend and want to wait for response until the queued process is finished. 很好地了解了您之后,您便向后端发送了一个请求,并希望等待响应,直到排队的进程完成。 The process can (must not) take also a long time, like 5 minutes. 该过程也可以(必须)花费很长时间,例如5分钟。 And here is the problem, your request on backend will be closed after some time for timeout reasons. 这就是问题所在,由于超时原因,您在后端的请求将在一段时间后关闭。

The right way should be, 正确的方法应该是

  1. you send the request to your backend and get success response on successful queued request or failure on problems queuing your request. 您可以将请求发送到后端,并在成功排队的请求中获得成功响应,或者在排队请求时遇到问题而失败。
  2. After some time you can send second request to the backend to retrieve if the process has been done. 一段时间后,您可以向后端发送第二个请求,以检索该过程是否已完成。 This request can be send multiple times until all user queued request has been processed. 可以多次发送此请求,直到处理完所有用户排队的请求为止。

An other possible solution would be the usage of socket.io. 另一种可能的解决方案是使用socket.io。

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

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