Use synchronous request in jQuery.ajax
Khanh is a full stack web developer with over 15 years of experience developing for the web
Search for a command to run...
Khanh is a full stack web developer with over 15 years of experience developing for the web
No comments yet. Be the first to comment.
Elevate your bus reservation experience (or even movie, theater, train, ship, plane tickets) with the React Bus Seat Map Picker – a sophisticated and meticulously crafted script designed exclusively for bus seat bookings. Engineered with strict TypeS...

In previous blog, I migrated all posts from my WordPress to Hashnode at https://blog.donamkhanh.com. Now I create a simple landing page for my main domain and hosted on Github Page. You can access at https://donamkhanh.com. In feature, I'm going to h...

I just finished migrating my blog from Wordpress (self hosted on DigitalOcean) to Hashnode. How did I migrate? I don't have too many articles on Wordpress but I don't like doing it manually so the first thing I did was look to see if there was an aut...

Dự án mình đang làm có yêu cầu sử dụng Azure Active Directory (AAD) để lưu trữ thông tin người dùng & xác thực đăng nhập các kiểu. Ngoài các trường thông tin cơ bản mà AAD hỗ trợ ra thì dự án mình cần lưu thêm 1 số thông tùy chọn khác cho người dùng....

Introduction This is a concise front-end application developed with Angular. You can create a beautiful form with drag and drop visualization. You can get inspiration from it to make a draggable form; at the same time, you can also modify it to suit ...

Đã bao giờ bạn sử dụng AJAX (jQuery) để check username tồn tại như sau chưa?
function checkUser(u) { var isExist = false; $.post( 'index.php', {'username': u}, function(ret) { isExist = ret; } );
return isExist; }
if( checkUser('donamkhanh') ) { alert('donamkhanh already exist'); } else { alert('donamkhanh does not exist'); }
Theo bạn, nếu chạy đoạn mã trên thì sẽ ra kết quả nào (giả sử rằng server script hoàn toàn chạy chính xác, trả về giá trị true/false hay 1/0 gì đó & username donamkhanh đã tồn tại)?
Do mặc định trong jQuery tất cả các request đều được sent asynchronous nên trong quá trình send request thì hàm checkUser đã return luôn giá trị lúc khởi tạo biến isExist = 0 rồi. Vậy nên kết quả luôn thông báo là username donamkhanh chưa tồn tại :)
Cách fix rất đơn giản, sử dụng thuộc tính async của $.ajax. Các hàm $.post hay $.get không có thuộc tính này vì
This is jQuery's low-level AJAX implementation. See $.get, $.post etc. for higher-level abstractions that are often easier to understand and use, but don't offer as much functionality (such as error callbacks).
Code ở trên sẽ sửa lại như sau:
function checkUser(u) { var isExist = false; $.ajax({ async: false, type: "POST", url: "ajax.php", data: "username=" + u, success: function(msg){ isExist = msg; } });
return isExist; }
Nếu set async = false, jQuery sẽ sử dụng synchronous cho các request. Ở ví dụ này bạn có thể thấy nó chưa thực sự cần thiết (vì có thể nhét đoạn check vào luôn trong callback function của $.post, $.get hay bất cứ ajax method nào của jQuery), tuy nhiên nếu bạn phải viết nhiều hàm (ajax) và các hàm này lấy kết quả của nhau để tính toán thì việc set synchronous là bắt buộc. Nếu không thì sẽ xảy ra tình trạng nhiều biến nhiều hàm lon ton cầm đèn dầu chạy trước ô tô ^^