I am trying to make an ajax calling (using jquery) and render the responses from the server as soon as they are arriving. In the server I send the different responses as soon as they are ready. If I make a request using curl I can see how they are going printing in screen response by response. But I can't do something similar using jquery ajax.
The code on the server is like this:
flusher, ok := app.Response.Out.(http.Flusher)
if !ok {
panic("expected http.ResponseWriter to be an http.Flusher")
}
app.Response.Out.Header().Set("Connection", "Keep-Alive")
app.Response.Out.Header().Set("Transfer-Encoding", "chunked")
app.Response.Out.Header().Set("X-Content-Type-Options", "nosniff")
fmt.Fprintf(app.Response.Out, string(JsonOffers))
flusher.Flush()
This snippet of code is calling into a loop. app.Response.Out
is a http.ResponseWriter
.
The request I do, is a normal jquery.ajax function:
jQuery.ajax({
url: '/ndc',
type: "POST",
data: ODRequest,
success: function(offers){
console.log( 'Offers', offers )
},
error: function(x,y,z){console.log("ERROR", x);}
})
With this I get all the offers, but only when the last response is processed, and not one by one.
One thing more... (I don't know if it could be important) on the server console I can see this, in each request:
server.go:2161: http: multiple response.WriteHeader calls
Please, describe the procedure more precisely, your application works the same way, like this?
Js script should send a request to the server, and when the response is received is again send a request.
var ajaxFunc = function () {
// handle response and again call
ajaxFunc();
};
When the server receives a request, it must run an loop for a certain time (may be 20-30 seconds), and send updates to the client