updating database with ajax- handling with errors - php

Im building a website where the user uses ajax calls in order to update the database. Its works well on xammp but im worried that problems may happen frequently when the site will be on the net.
i've got some questions:
If ajax fails (and my code is good), is it always because of connections?
Can an attempt fail but succeed later?
What is the best way to handle those failed requests?

If ajax fails (and my code is good), is it always because of connections?
An Ajax call can fail for a variety of reasons. Reasons can be, but are not limited to: Javascript error, Network Timeout, Serverside error, JS Performance, Browser settings, etc
Can an attempt fail but succeed later?
If your AJAX request fails it will not automaticly retry. You need to program that yourself.
What is the best way to handle those failed requests?
You should have error handling in javascript to check for the errors. Basicly there are 3 cases:
Correct response from server
Response from server, but with incorrect return (failed serverside)
No Response from server (failed clientside)
You can either notify the user of a failed request or retry a few times and notify the user later, if the request never completes.

Related

Is there a way to track requests sent to couchbase server via sdk

I wanted to know if there is a way to track what requests being sent to couchbase server via sdk.
For example: If I am sending and upsert() / save() request to save the document, then is there a way to track that request as to what is being sent and if there are any issues where exactly the problem lies.
There is not any particular way to track the specific requests that an SDK is dispatching to the server. You should be getting any errors that occur back as a response through the language though. For PHP this would mean that the operation will be throwing an exception, and in Node.js you will get the error back as the first parameter of the callback function.

How to debug a "pending" HTTP request to a PHP web app?

I'm maintaining an old CodeIgniter web app.
Lately, when certain forms are saved, the browser shows a permanent "Pending" for the status of the request. The request never completes (unless I reboot the web server in which case it errors out immediately).
There aren't any client-side errors in Dev Tools.
There aren't any server-side errors in the logs.
This happens in different browsers.
It happens in a Chrome incognito window with no extensions loaded.
All I have to go on is, the submit button it hit, the browser says "waiting for <servername>" and nothing else happens.
Does this look like a network problem? There was no problem loading any of the application's other pages, all of which came out of the same database and web server.
How do I debug a "pending" HTTP request in a browser, when neither the client or the server shows an error?
TIA
You can rule out a network problem because the other sites are working. If the same site sometimes works and sometimes won't there could be a network problem but this isn't very likely.
The fact that the request is pending and there aren't any errors means that the server accepted the request and is handling it. When there is no error the server handles it until either an error appears or the response is returned by the php processor.
This brings me to the assumption that the problem is an endless loop or a long running function in your serverside code.
error detection procedure
First check that the error reporting is enabled in index.php.
Now you could wait wait till the code runs in a timeout and throws an error
and/or
you could go to the config/routes.php and check which controller and function handles the request and break down the functions and test which one takes this long.
If you need further help please paste some code.

File upload script apparently not executing until upload has completed

I have an odd problem: I've got an AJAX file uploader (fineuploader, as it happens), with a server-side script (PHP) that handles the upload. Although the uploader has an "allowedExtensions" setting, I want the allowed extensions to be specified on the server side, so my upload handler checks the file extension and returns an error response if the file's extension is not allowed.
The problem I'm having is that whereas in my dev environment the upload handler returns this response straight away (as expected) and the upload stops, on another server the upload goes ahead and the response is only returned after the upload has completed. Which potentially means a very long wait before receiving an error message.
Looking at the network info in developer tools, it seems the browser is waiting for a response the whole time, and (on the problematic server) only receives it after the upload has completed, which seems to suggest that on the server the upload handler script is actually not being executed until after all the data has been received.
It seems to me that the most likely culprit is some setting to do with how PHP handles uploads / multi-part form data, but I can't figure out what that might be. Would be grateful for any advice!
Update:
It seems the problem is the same on both servers (I just didn't notice the lag on one). So it seems my upload handler script is not executing until the file transfer has completed (this seems likely because the script checks the filename early on and throws an Exception if the extension is wrong, so it should respond quickly once it's started. Also, it always responds almost immediately when the upload has completed - however long that takes - suggesting that's when it's being executed).
Is this just a feature of how PHP handles multi-part form data? Is there any way of allowing the script to respond immediately if the filename is unsuitable?
It could be as simple as the dev server being really fast to receive the file and respond as fast. If the dev server is on your own machine or a local dev server, 100mbits connexion makes even the pretty large file blazingly fast while on a production server that is often outside of the current network, the upload is long...
Sadly no, it's not possible for PHP to respond before the request being complete because thats the very nature of HTTP. You can cut off the connection once the request is sent and not read the response but you can't expect to receive a response until the whole request is sent. Although i used to do this with my friends, that is cut them off and answer before the end of the question, i can ensure you that only humans are capable of that feat! Oh and dont do that, it breaks friendships
o multipart doesn't mean chunked upload, it means the message is separated into different parts using a message boundary to separate the different element. If the process started before the whole request was sent anyway, you'd need to integrate mechanisms to detect if a certain part of your request was completely uploaded which would make the web even harder to program than it is now!
You can look at this for an example of what a multipart request looks like htmlcodetutorial.com/forms/form_enctype.html

Handle Multiple request from clients to a PHP page with MySQL

i have a Ubuntu server and a mobile app, i make request with JSON from the mobile app to a php page on the server that handle the request connecting to a MySql DB, but i notice that sometime happen that when the request are a lot in the same time, some request drop, i want know if there is a way to handle multiple request, without lose some.
When do the requests drop? Whilst sending a SQL query to the Mysql database?
The cause of this can be either the server itself, which just can't handle that many requests at a time (probably just configured to do so), the script you're using (badly written) or the database.
After you find the cause, start looking for the cure.

Issues with PHP cURL between servers, return transfer probably wont catch the response

I have a really weird behavior going on.
I'm hosting a tracking software for users, that mainly logs mobile traffic. Now, the path is as follows:
1. My client gets a php code snippet to put in his website.
2. This code sends a cURL post (based on predefined post fields like: visiotr IP, useragent, host etc) to my server.
3. my server logs the data, and decide what the risk level is.
4. it then responds the client server about the status. That is, it sends "true" or "false" back to the client server.
5. client server gets that r
esponse, and decides what to do (load diffrent HTML content, redirect, block the visitor etc).
The problem I'm facing is, for some reason, all the requests made from my client's server to my server, are recorded and stored in the a log file, but my clients report of click loss as if my server sends back the response, but their server fails to receive those responses or something.
I may note that, there are tons of requests every minute from different clients' servers, and from each client himself.
Could the reason be related to the CURL_RETURNTRANSFER not getting any response ? or, maybe the problem is cURL overload ?
I really have no idea. My server is pretty fast, and uses only 10% of its sources.
Thanks in advance for your thoughts.
You touched very problematic domain - high load servers, you problem can be in so many places, so you will have to really spend time to fix it, or at least partially fix.
First of all, you should understand what is really going on, check out this simplified scheme:
Client's php code tries to open connection to your server, to do this it sends some data via network to your server
Your server (I suppose apache) tries to accept it, if it has resources - check max connections properties in apache config
If server can accept connection it tries to create new thread (or use one from thread pool)
After thread is started, it runs your php script
Your php script do some work, connecto to db and sends response back via network
Client waits till the answer from p5 or closes connection because of timeout
So, at each point you can have bottleneck:
Network bandwidth
Max opened connections
Thread pool size
Script execution time
Max database connections, table locks, io wait times
Clients timeouts
And it is not a full list of possible places where problem can occur and finally lead to empty curl response.
From the very start I suggest you to add logging to both PHP codes (clients and servers) and store all curl_error problems in some text file, at least you will see what problems occur often.

Categories