I recently perform some ajax request on my website but I'm still beginner.
Here is a stupid question: How to send (POST) a data to the server without waiting any response ?
In all the example I found on internet, the ajax call is used to reload a part of the page. It work perfectly, but my idea here is just to send a data that could be store in DB for example without waiting any response.
I'm using Zend framework with MVC pattern. I tried something like:
$.ajax({
type: "POST",
url:'/controller/action',
data: { myDataToPost: aData },
success:function(response){
},
failure:function(){
alert('Could not save your entry');
}
});
It works fine if I define an action in my controller but I can see in firebug an 404 error on controller/action page. For sure it doesn't exist because i don't want any response...
Many thanks for your help!
Cedric.
"Here is a stupid question: How to send (POST) a data to the server without waiting any response ?"
$.ajax({
timeout: 1
});
Set the request timeout to 1 millisecond (zero doesn't work), thus giving the xmlHttpRequest 1 millisecond to complete (go to server, process request, return response, which is virtually impossible), otherwise mark it as timed out (failed).
Related
I have a browser-based client that makes AJAX calls to a PHP/Apache back-end. When a button is clicked, the client loops through a list of "providers" and sends an Ajax request to each of them with exactly the same payload.
Randomly some of the requests in the loop are arriving at the server side with a blank POST body. What is really strange about this is the fact that other requests in the same loop are arriving properly populated. Sometimes we are seeing all of the items in the loop arriving with a blank body, sometimes some in the loop. It is really strange!
Environment details:
Client = JQuery 3.3.1, JQueryUI 1.12.1, Bootstrap 3.3.7, JQuery validate 1.19.0
Server = Apache 2.4, PHP 7.1
We cannot actually reproduce the problem in a development environment, but logging in production is clearly showing that this is happening regularly on a daily basis (maybe 5% of requests). We have deployed monitoring on the client browsers to trap any errors and there is nothing in terms of exceptions or anything that looks like it could be related.
The sample below shows what we are doing on the client side.
1) bind a click event to
2) when clicked, populate a request object
3) loop through the list of "providers", posting request from 2 to PHP server
$(document).ready(function() {
bindEvents();
}
function bindEvents() {
$("#myButton").click(function(event) {
event.preventDefault();
handleClick();
});
}
function handleClick() {
var request = ...;
...
var save=false;
if (....) {
save=true;
}
var providers = ["provider1", "provider2", "provider3", "provider4", "provider5", "provider6"];
var ndx;
for (ndx=0; ndx<providers.length; ++ndx) {
doRequestSend("action/"+(save?"save":"verify"), request, providers[ndx]);
}
}
function doRequestSend(action, request, provider) {
request['apiKey']=getAPIKey();
var url=baseURL+"/api/"+provider+"/"+action;
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(request),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
...
},
failure: function(errMsg) {
...
},
error : function(jqXHR, textStatus, errorThrown) {
...
}
});
}
Obviously, it is difficult to figure out the issue when I cannot reproduce. However, am hoping the someone might spot a problem with the approach that could be resulting in the issue.
Alternatively, open to any random/crazy ideas that could explain HOW this could possibly be happening! Some requests in the loop arrive fine, others arrive blank!
Notes:
- Although the loop POST to a slightly different url with each iteration, on the server side, they all end up at the same entry point and the first thing that is done is a log of the body (this is how we know it is blank!)
- The only browsers that (to date) seem to have experienced the issue are Chrome 73 & 74 on Windows. This might not be relevant!
Anyone got any ideas or things to try that might track down the source?
Cheers!
I have a PHP script on my server that needs to be run from my clients websites using Javascript in a plain HTML page. After the script is run the HTML page will redirect. The problem is that sometimes the script doesn't run before the redirect happens.
This is the code I am using...
$.ajax({
async: false,
type: 'GET',
url: 'the_URL_of_the_PHP_on_my_server.php',
success: function(data) {
}
});
window.location="the_URL_for_the_redirect";
The PHP script on my server is to track hits/sales etc. Is there are way I can force the page to wait for the script to complete before the page redirect.
The HTML page and the PHP page are on different servers. Also, the HTML page is being used on lots of different websites, so I can't give them all permission to access my server. I'm not sure if that's causing a problem or not.
I don't need any information back from the PHP script I just need it to run.
Thank you.
The success function runs when you get a response (unless it was an error, in which case the error function you haven't defined would run).
If you want some code to run after you get a response, put it inside those functions instead immediately after the code which sends the request.
That said: The point of Ajax is to talk to the server without leaving the page. If you are going to go to a different page as soon as you have a response, then don't use Ajax. Use a regular link or form submission and then having an HTTP redirect as the response.
This is normal, that this situation happens.
because $.ajax is async and won't wait till success method
change your code to
$.ajax({
async: false,
type: 'GET',
url: 'the_URL_of_the_PHP_on_my_server.php',
complete: function(data) {
window.location="the_URL_for_the_redirect";
}
});
UPDATED
changed function success to complete
difference is =>
complete will be called not matters what happened with request
UPDATE 2
think about #Quentin variant by html redirects.
I have a php script that I invoke via an ajax call with jQuery:
$.ajax({
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
type: "POST",
url: "getFares.php",
data: someObjectHere,
success: function(data) {
handleSuccess(data);
},
dataType: "json"
});
Most of the time this request works just fine and the server sends the values that I would expect.
Sometimes however, the server just responds with a 303 SEE OTHER response. Nowhere in my php script is anything that could produce this redirect.
Unfortunately I have not been able to find any kind of pattern when the redirect happens. It appears like it only happens when I load the page, then wait for a bit and then invoke the ajax request, but this might be coincidence.
I know this is not a very helpful description but unfortunately I'm stuck here so I'm hoping that someone by luck knows how to fix it.
Here are a couple of screenshots of the dev tools that should illustrate the problem more clearly:
In this image you can see that I've made a couple of request to the script (getFares.php) and I've highlighted one that worked. You can see the status code is 200 and everything is fine.
Here I've highlighted a failed request. The response is 303 See other. As you can see, none of the other values of the request have changed
The only difference in the requests that I have been able to find is seen here. For requests that return the correct result (status 200), the type is "xhr" and for requests that result in the redirect are of type "x-www-form-urlencoded; charset=UTF-8". I don't know why this happens or where it comes from.
I assume the problem could be related to faulty server/php settings but it's difficult to search for this kind of error.
It is not ajax issue but the server itself sending response for some reason.
getFares.php page must be checked for some post parameters, if fulfills, sends 200 response otherwise some other response like 303.
You can check getFares.php code to get more idea.
I'm making a AJAX request like this:
$("#botao_pesquisar_material").click(function(){
$(this).attr("disabled", "disabled");
$("#material").addClass('loading');
$.ajax({
url: '{{ URL::base() }}/material/lista_ajax/',
dataType: 'json',
cache: false,
error: function(data)
{
console.log(data);
},
success: function(data) {
for(x in data)
{
console.log(data[x]);
}
}
});
My PHP method is ok because when I access it via URL, it returns me the expected JSON and when I call this on localhost, it works perfectly so it isn't about special characters or something else with the data.
When I run this on my server I have this:
GET http://dc.portaldeideias.com.br:8080/sergios/public/material/lista_ajax/?callback=jQuery172007718208665028214_1342725644520&_=1342725649090 500 (Internal Server Error) jquery.js:4
f.support.ajax.f.ajaxTransport.send jquery.js:4
f.extend.ajax jquery.js:4
$.click.$.ajax.url
f.event.dispatch jquery.js:3
f.event.add.h.handle.i
Using console.log(data) I have the entire JSON response but here is what is really strange:
The readyState is 4
The responseText is [{"attributes":{"pk_a030_id":78,"a030_descricao":"Camur\u00e7a"},"original":{"pk_a030_id":78,"a030_descricao":"Camur\u00e7a"},"relationships":[],"exists":true,"includes":[]}]
The statusText is Internal Server Error
So, the requested value is created but I receive a Internal Server Error
Sorry for posting this as an answer, but I don't have the required privileges to add this as a comment for your question.
Have you noticed that the URL in the javascript log http://localhost:8080/sergios/public/material/lista_ajax/ is different than the one provided in your screenshot http://dc.p[...]s.com.br:8080/sergios/public/material/lista_ajax?
Could it be the case that you have two different versions of the lista_ajax PHP method hosted in two different servers (maybe one remote and the other one local), and that's why it works flawless when seeing it from the browser and has bugs when tested with ajax?
It's also important to notice that if you are browsing a website hosted on a remote server, and the ajax is configured to a localhost address, it will do a request for your own machine, not the remote server (javascript runs in the browser, so localhost translates to its own client address).
Maybe this is not the case, but it was worth commenting.
I have made a simple chat application which uses long-polling approach using jquery,
function sendchat(){
// this code sends the message
$.ajax({
url: "send.php",
async: true,
data: { /* send inputbox1.value */ },
success: function(data) { }
});
}
function listen_for_message(){
// this code listens for message
$.ajax({
url: "listen.php",
async: true,
timeout:5000,
success: function(data) { // the message recievend so display that message and make new request for listening new messages
$('#display').html(data);
listen_for_message();
}
});
}
THIS SHOULD HAPPEN : after page loaded the infinite request for listen.php occurs and when user sends message, the code sends message to database via send.php.
PROBLEM is, using firebug i've found that send.php request which is performed after listen.php request, is remains pending. means the request for send message is remains pending.
The issue was because of session locking;
both send.php and listen.php files use session variables,
so session is locked in listen.php file and the other file (here send.php file) can't be served after the session frees from serving another file ( here listen.php).
How do I implement basic "Long Polling"?
the link above is a similar question that may help you.
it does not have to be on a database, it can be saved on a tmp file, but your problem is that you are choking the browser by performing too many requests, any one browser handles two requests at a time, which means you should really allow the browser to finish the first requests first then do the second one... and so on...
you do not need to do send.php and listen.php, because you can do it simply on one page both of them.
function check(){
$.ajax({
url : 'process.php',
data : {msg:'blabla'/* add data here to post e.g inputbox1.value or serialised data */}
type : 'post',
success: function (r){
if(r.message){
$('#result').append(r.message);
check();//can use a setTimeout here if you wish
}
}
});
}
process.php
<?php
$msg = $_POST['msg'];//is blabla in this case.
$arg['message'] = $msg;//or grab from db or file
//obviously you will have to put it on a database or on a file ... your choice
//so you can differentiate who sent what to whom.
echo json_encode($arg);
?>
obviously this are only guide lines, but you will exhaust your bandwidth with this method, however it will be alot better because you have only one small file that returns either 0 to 1 byte of information, or more if there is a message posted.
I have not tested this so don't rely on it to work straight away you need a bit of changes to make it work but just helps you understand how you should do it.
however if you are looking for long pulling ajax there are loads of scripts out there already made and fine tuned and have been test, bug fixed and many minds help built it, my advice is don't re-invent the wheel