Because AJAX is executed asynchronously by default, that is, the next line of JS statement is executed without waiting for the request to finish, resulting in null assignment.
Solution:
Add the following code:
$.ajax({
async:false,
//******other codes
});
It can be solved.